I have the following java class that generates WAV audio file from PCM data. I want to implement the same functionality in JavaScript or NodeJS equivalent. Is there any existing library you can recommend?
There's an existing NPM library out there called random-access-file but it is limited.
SampleAudioExport.java
package com.example.sampleapp;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.LinkedBlockingQueue;
public class SampleAudioExport implements Runnable {
public final LinkedBlockingQueue<short[]> samples = new
LinkedBlockingQueue<>();
public String audioDir;
private Thread thread;
public boolean isRunning = false;
public boolean isDone = false;
public File wav;
private final RandomAccessFile randomAccessWriter;
private int payloadSize;
public SampleAudioExport(String dir) throws IOException {
payloadSize = 0;
audioDir = dir;
String fileName = "recording.wav";
wav = new File(audioDir + "/" + fileName);
int sRate = 44100;
short nChannels = 2;
int mBitsPerSample = 16;
randomAccessWriter = new RandomAccessFile(wav.getAbsoluteFile(), "rw");
randomAccessWriter.setLength(0); // Set file length to 0, to prevent unexpected behavior in case the file already existed
randomAccessWriter.writeBytes("RIFF");
randomAccessWriter.writeInt(0); // Final file size not known yet, write 0
randomAccessWriter.writeBytes("WAVE");
randomAccessWriter.writeBytes("fmt ");
randomAccessWriter.writeInt(Integer.reverseBytes(mBitsPerSample)); // Sub-chunk size, 16 for PCM
randomAccessWriter.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
randomAccessWriter.writeShort(Short.reverseBytes((short) 2));// Number of channels, 1 for mono, 2 for stereo
randomAccessWriter.writeInt(Integer.reverseBytes(sRate)); // Sample rate
randomAccessWriter.writeInt(Integer.reverseBytes(sRate*nChannels*mBitsPerSample/8)); // Byte rate, SampleRate*NumberOfChannels*mBitsPersample/8
randomAccessWriter.writeShort(Short.reverseBytes((short)(nChannels*mBitsPerSample/8))); // Block align, NumberOfChannels*mBitsPersample/8
randomAccessWriter.writeShort(Short.reverseBytes((short) mBitsPerSample)); // Bits per sample
randomAccessWriter.writeBytes("data");
randomAccessWriter.writeInt(0); // Data chunk size not known yet, write 0
}
public void start() {
this.isRunning = true;
this.thread = new Thread(this);
this.thread.start();
}
public void stop() {
this.isRunning = false;
}
public void pushAudio(short[] sample) {
this.samples.add(sample);
}
#Override
public void run() {
try {
Log.d("AudioExport", "RUNNING AUDIO EXPORT");
Log.d("AudioExport", "Samples=" + this.samples.size());
while (Thread.currentThread() == this.thread && this.samples.size() > 0) {
short[] sample = this.samples.take();
byte[] buffer = this.toByteArrayForAudioData(sample);
randomAccessWriter.write(buffer);
payloadSize += buffer.length;
}
randomAccessWriter.seek(4); // Write size to RIFF header
randomAccessWriter.writeInt(Integer.reverseBytes(36 + payloadSize));
randomAccessWriter.seek(40); // Write size to Subchunk2Size field
randomAccessWriter.writeInt(Integer.reverseBytes(payloadSize));
randomAccessWriter.close();
isRunning = false;
isDone = true;
Log.d("Audio", "AUDIO EXPORT DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
public byte[] toByteArrayForAudioData(short[] shorts) {
byte[] output = new byte[shorts.length * 2];
for (int i = 0; i < shorts.length; i++) {
output[i * 2] = (byte) (shorts[i] & 0xff);
output[(i * 2) + 1] = (byte) ((shorts[i] >> 8) & 0xff);
}
return output;
}
}
Related
I want to build a function that sign a SHA512 HMAC with a Private key with a password to open that file. I have done that with Javascript but I'm not able to do it in JAVA. here is my code in Javascript:-
const fs = require('fs')
const crypto = require('crypto');
const getSignatureByInput = (input, crt) => {
let privatePem = fs.readFileSync(crt);
let key = privatePem.toString('ascii');
let sign = crypto.createSign('RSA-SHA512');
sign.write(input)
let signature = sign.sign({ key, passphrase: 'Password' }, 'hex')
return signature;
}
// Usage
let hamc = crypto.createHmac("sha512", '12345678-73a8-414c-9832-fcd4b2d9b362').update(
JSON.stringify({
"body": {
"Id": "123"
}
})
).digest("hex");
console.log(hamc)
let signatureSignedByPrivateKey = getSignatureByInput(hamc,'./private.key');
console.log(signatureSignedByPrivateKey)
and the below is the example of my private key:-
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIHKTAbBgoqhkiG9w0BDAEDMA0ECATHx+tZ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XxpJ+t4+Splat/9Lw/CJHe37JHO1TmHPXr
IOSbwnCybydr7S/4mRYOJOm8l2BMdPb7IJUX43xI2gnf3NDPidHxz6aiO1XniSWu
/lEsOsmTv7jKjJnObwIAPEzJBkhp9g5Y3gkENkkEIFK/OD8tyTS7b1wsqOU8d4HD
XYd6+WWZhnTG9hixaIqK3PytVAZusB1GAd/ZzukC8U208zN9ud5LRQFIicdXhBFX
amtrYkTn+gCXRax0eDHVi3FohpySKqC22wyLe1C1VC3HvZ+HhZMDt3Hyt1ZeFWHr
XfJTekUHHwFnpKVang==
-----END ENCRYPTED PRIVATE KEY-----
Appreciate your support!
Thanks in advance...
I found a way to solve this thanks #pra-a for answering a question posted earlier Link here.
I will share my solution below.
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.*;
import java.security.spec.*;
public class Main {
public static final String PRIVATE_KEY="./private.der";
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
// write your code here
//get the private key
File file = new File(PRIVATE_KEY);
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) file.length()];
dis.readFully(keyBytes);
dis.close();
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKey privKey = (RSAPrivateKey) kf.generatePrivate(spec);
Mac sha512Hmac;
String result = "";
final String key = "'12345678-73a8-414c-9832-fcd4b2d9b362'";
String signature = "";
String body = "{\n" +
" \"body\": {\n" +
" \"Id\": \"123\"\n" +
" }\n" +
"}";
try {
final String HMAC_SHA512 = "HmacSHA512";
final byte[] byteKey = key.getBytes(StandardCharsets.UTF_8);
sha512Hmac = Mac.getInstance(HMAC_SHA512);
SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512);
sha512Hmac.init(keySpec);
byte[] macData = sha512Hmac.doFinal(body.getBytes(StandardCharsets.UTF_8));
// Can either base64 encode or put it right into hex
//result = Base64.getEncoder().encodeToString(macData);
result = bytesToHex(macData);
signature = sign(result, privKey);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// Put any cleanup here
System.out.println("Done");
}
}
private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
public static PrivateKey readPrivateKeyII(String filename) throws Exception{
InputStream in = new FileInputStream(filename);
byte[] privateKeydata = new byte[in.available()];
in.read(privateKeydata);
in.close();
KeyFactory privateKeyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(privateKeydata);
PrivateKey privateKey = privateKeyFactory.generatePrivate(encodedKeySpec);
return privateKey;
}
public static String sign(String plainText, PrivateKey privateKey) throws Exception {
Signature privateSignature = Signature.getInstance("SHA512withRSA");
privateSignature.initSign(privateKey);
privateSignature.update(plainText.getBytes(StandardCharsets.UTF_8));
byte[] signature = privateSignature.sign();
return bytesToHex(signature);
}
}
My purpose is to display pdf use pdf.js in lazy mode,I have two choice:
Use disableRange=false
It worked fine when use a url in Nginx, but when I use a java servlet url: /dowload/fileid/123,it doesn't load via 206 partial content (range requests) but 200 and then viewed in the viewer.
class Scratch {
public static void main(String[] args) {
public void download (String identNo, HttpServletRequest request, HttpServletResponse response){
File file = getFileFromServer(identNo);
BufferedInputStream bis = null;
OutputStream os = null;
BufferedOutputStream bos = null;
InputStream is = null;
try {
is = new FileInputStream(file);
bis = new BufferedInputStream(is);
os = response.getOutputStream();
bos = new BufferedOutputStream(os);
int startByte, endByte, totalByte;
if (request != null && request.getHeader("range") != null) {
String[] range = request.getHeader("range").replaceAll("[^0-9\\-]", "").split("-");
totalByte = is.available();
startByte = Integer.parseInt(range[0]);
if (range.length > 1) {
endByte = Integer.parseInt(range[1]);
} else {
endByte = totalByte - 1;
}
response.setStatus(206);
} else {
totalByte = is.available();
startByte = 0;
endByte = totalByte - 1;
response.setHeader("Accept-Ranges", "bytes");
response.setStatus(200);
}
int length = endByte - startByte + 1;
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + totalByte);
response.setContentType("Content-Type: application/octet-stream");
response.setContentLength(length);
bis.skip(startByte);
int len = 0;
byte[] buff = new byte[1024 * 64];
while ((len = bis.read(buff, 0, buff.length)) != -1) {
if (length <= len) {
bos.write(buff, 0, length);
break;
} else {
length -= len;
bos.write(buff, 0, len);
}
}
} catch (IOException e) {
} finally {
FileUtil.closeQuiet(bos);
FileUtil.closeQuiet(os);
FileUtil.closeQuiet(bis);
FileUtil.closeQuiet(is);
}
}
}
}
Split big pdf file in server side,then display multiple pdf documents as one with pdf.js
I found this: Is there a way to combine PDFs in pdf.js?
but it needed to load all file at once, what I need is: when scroll to bottom of file, load next file then merge to current pdf
PDF.js,set the parameter: "disableAutoFetch": true, "disableStream": true
server support accept ranges,range
Then pdf.js whill only fetch data in pages are visible now from server
https://github.com/mozilla/pdf.js/issues/11933
Um so basically I'm trying to upload a file from my android phone to app script which will create the file in google drive. This works will when accessing via browser. But when trying to do a multipart/httppost/formdata, it doesn't work. the params AFAIK seems to be correct. Posting Strings work without any issue (tried with volley library), however files/images don't seem to work. Adding Google drive SDK is not an option for me since most of my app users are going to be using custom custom OS, hence google play services not working correctly.
Current code for app script
server.gs
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html');
}
function uploadFiles(form) {
try {
var dir = "Files";
var folder, folders = DriveApp.getFoldersByName(dir);
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dir);
}
var blob = form.myFile;
// var file = folder.createFile("BlobTest","test");
var file = folder.createFile(blob);
file.setDescription("Uploaded by " + form.myName);
return file.getId() ;
} catch (error) {
return error.toString();
}
}
**Form.html**
<form id="myForm">
<input type="text" name="myName" placeholder="name..">
<input type="file" name="myFile" >
<input type="submit" value="Upload File"
onclick="this.value='Uploading..';
google.script.run.withSuccessHandler(fileUploaded)
.uploadFiles(this.parentNode);
return false;">
</form>
<div id="output"></div>
<script>
function fileUploaded(status) {
document.getElementById('myForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
<style>
input { display:block; margin: 20px; }
</style>
**And my android code**
private class uploadimg extends AsyncTask<Void, Integer, Integer> {
int serverResponseCode = 0;
String serverResponseMessage;
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Integer doInBackground(Void... params) {
return uploadFilez();
}
public int uploadFilez() {
String sourceFileUri = selectedImagePath;
String upLoadServerUri = url;
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
return 0;
}
try { // open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("myFile", fileName);
// conn.setRequestProperty("Accept-Charset", "UTF-8");
// String k ="";
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name="+"tits"+";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer of maximum size
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
serverResponseMessage = conn.getResponseMessage();
if(serverResponseCode == 200){
runOnUiThread(new Runnable() {
public void run() {
}
});
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
dialog.dismiss();
return serverResponseCode;
}
#Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
}
}
any help is appreciated :) thank you
How can I read the Simcard serial number and use the first 16 numbers
as a SecretKey
........
private String SecretKey = "0123456789abcdef";**//Read the Simcard serial number and use the first 16 numbers as a SecretKey**
public MCrypt()
{
ivspec = new IvParameterSpec(iv.getBytes());
keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
................
what I'm doing wrong ??
public class MCrypt {
static char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
private String deviceid = telephonyManager.getSimSerialNumber();
//private String deviceid = "894310210159403811111456457657845786485458";
private String serial = new String (deviceid.substring(0, 16));
private String iv = ""+serial;//Dummy iv (CHANGE IT!) 8943102101594038
private static IvParameterSpec ivspec;
private static SecretKeySpec keyspec;
private static Cipher cipher;
private static Context context;
private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!)
O.k. now I see your problem.
Activity inherit's from Context - so when you are calling getSystemService in your class MCrypt , you are really wanting to call super.getSystemService.
If you want to have a Context available in another class (like MCrypt), you can pass it in as an argument to a method of that class (the constructor, which you already have), and keep a reference to it, etc.
Add Context argument to your constructor in MCrypt:
public class MCrypt {
Context mContext;
TelephonyManager mtelemamanger;
public MCrypt (Context context)//this is a constructor
{
mContext = context;
mtelemamanger = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
...
Add the below permission into your Androidmanifest.xml file.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Then use this:
TelephonyManager telemamanger = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = telemamanger.getSimSerialNumber();
the getSimSerialNumber() function returns a string which contains provider info etc. If you want to get the pure sim serial number you have to get the last 14 digits.
String SecretKey = deviceid.substring(Math.max(0, deviceid.length() - 14));
Thats my Worked Code
/**
* Created by Studio on 02/06/2017.
*/
import android.content.Context;
import android.telephony.TelephonyManager;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class MCrypt {
TelephonyManager mtelemamanger;
private static char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
private static IvParameterSpec ivspec;
private static SecretKeySpec keyspec;
private static Cipher cipher;
MCrypt(Context con)
{
mtelemamanger = (TelephonyManager) con.getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = mtelemamanger.getSimSerialNumber();
String iv = deviceid.substring(0, 16);
ivspec = new IvParameterSpec(iv.getBytes());
String secretKey = "0123456789abcdef";
keyspec = new SecretKeySpec(secretKey.getBytes(), "AES");
try {
cipher = Cipher.getInstance("AES/CBC/NoPadding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public byte[] encrypt(String text) throws Exception
{
if(text == null || text.length() == 0)
throw new Exception("Empty string");
byte[] encrypted;
try {
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
encrypted = cipher.doFinal(padString(text).getBytes());
} catch (Exception e)
{
throw new Exception("[encrypt] " + e.getMessage());
}
return encrypted;
}
static byte[] decrypt(String code) throws Exception
{
if(code == null || code.length() == 0)
throw new Exception("Empty string");
byte[] decrypted = null;
try {
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
decrypted = cipher.doFinal(hexToBytes(code));
//Remove trailing zeroes
if( decrypted.length > 0)
{
int trim = 0;
for( int i = decrypted.length - 1; i >= 0; i-- ) if( decrypted[i] == 0 ) trim++;
if( trim > 0 )
{
byte[] newArray = new byte[decrypted.length - trim];
System.arraycopy(decrypted, 0, newArray, 0, decrypted.length - trim);
decrypted = newArray;
}
}
} catch (Exception e)
{
throw new Exception("[decrypt] " + e.getMessage());
}
return decrypted;
}
public static String bytesToHex(byte[] buf)
{
char[] chars = new char[2 * buf.length];
for (int i = 0; i < buf.length; ++i)
{
chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
}
return new String(chars);
}
private static byte[] hexToBytes(String str) {
if (str==null) {
return null;
} else if (str.length() < 2) {
return null;
} else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i=0; i<len; i++) {
buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
}
return buffer;
}
}
private static String padString(String source)
{
char paddingChar = 0;
int size = 16;
int x = source.length() % size;
int padLength = size - x;
for (int i = 0; i < padLength; i++)
{
source += paddingChar;
}
return source;
}
}
I am using SQLite database for my PhoneGap project.The database is getting populated on every other OS I have tested except Android 4.4.0+ .
The Code for accessing database is below :-
public class MathWhiz extends CordovaActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
super.loadUrl(Config.getStartUrl());
SharedPreferences sp = getSharedPreferences("MYPREFS",
Activity.MODE_PRIVATE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// If no shared prefs exist, e.g. first install, it doesn't matter - the
// following will return false as a default
Boolean database_copied = sp.getBoolean("database_copied", false);
if (!database_copied) {
try {
String pName = this.getClass().getPackage().getName();
this.copy("Databases.db", "/data/data/" + pName
+ "/app_database/");
this.copy("sample.db", "/data/data/" + pName
+ "/app_database/myFile/");
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("database_copied", true);
editor.apply();
} catch (IOException e) {
e.printStackTrace();
}
}
}
void copy(String file, String folder) throws IOException {
File CheckDirectory;
CheckDirectory = new File(folder);
if (!CheckDirectory.exists()) {
CheckDirectory.mkdir();
}
InputStream in = getApplicationContext().getAssets().open(file);
OutputStream out = new FileOutputStream(folder + file);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
in.close();
out.close();
}
}
And this is how I am using the database :-
window.openDatabase("sampleDB", "1.0", "sample", 200000);
Can anybody please point out what updates I need to do in order to make it work on Android 4.4 + ? Thanks
try it.. its working well...
public boolean copyDataBaseFromAssets(Context c) throws IOException {
if(android.os.Build.VERSION.SDK_INT >= 17)
DB_PATH = context.getApplicationInfo().dataDir + "/databases/"+ DATABASE_NAME;
else
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"+DATABASE_NAME;
String pathToDatabaseFileInAssetFolder = DATABASE_NAME;
String pathToDatabaseFileInSystem = DB_PATH;
this.getReadableDatabase();
> getReadableDatabase funcation used in db import code
AssetManager assetManager = c.getResources().getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open(pathToDatabaseFileInAssetFolder);
} catch (IOException ex) {
return false;
}
if (inputStream != null) {
OutputStream outputStream = new FileOutputStream(pathToDatabaseFileInSystem);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
Log.d(TAG, "Database is copied");
return true;
}
return false;
}
Change this
this.copy("Databases.db", "/data/data/" + pName + "/databases/");
this.copy("sample.db", "/data/data/" + pName + "/databases/");