Hi guys i am having a audio file in assets folder and i need to save the same file onto sdcard .
How to acheive this.
Below is the code i am using to save file
String filename = "filename.txt";
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos;
byte[] data = new String("data to write to file").getBytes();
try {
fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// handle exception
} catch (IOException e) {
// handle exception
}
please help
try this
AssetManager mngr = getAssets();
InputStream path = mngr.open("music/music1.mp3");
BufferedInputStream bis = new BufferedInputStream(path,1024);
//get the bytes one by one
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
}
byte[] bitmapdata = baf.toByteArray();
After converting into byte array copy this into the sdcard as follows
File file = new File(Environment.getExternalStorageDirectory(), music1.mp3);
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write(bitmapdata );
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// handle exception
} catch (IOException e) {
// handle exception
}
Related
I need to save the .svg in the Internal storage of android application and retrieve it and set it to the ImageView.
I am not able to save the .svg file. i am using this method -
File cacheDir = ctx.getCacheDir();
f = new File(cacheDir, name + ".png");
try {
InputStream in = new java.net.URL(imageurl).openStream();
mIcon = BitmapFactory.decodeStream(in);
try {
FileOutputStream out = new FileOutputStream(
f);
mIcon.compress(
Bitmap.CompressFormat.JPEG,
100, out);
out.flush();
out.close();
return f;
} catch (FileNotFoundException e) {
return null;
} catch (IOException e) {
return null;
}
} catch (Exception e) {
return null;
}
Why are you decoding SVG into bitmap ? I'm not sure it is possible.
But if you want to save the SVG file to storage, just copy you input stream to the output stream.
Simple java solution :
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
or with IOUtils
If you want to display your SVG file, look at https://github.com/pents90/svg-android/tree/master/svgandroid
I want to encrypt video files stored in SD card
Environment.getExternalStorageDirectory()
I have found that Facebook conceal is good for encrypting large files. I have followed this tutorial Make fast cryptographic operations on Android with Conceal
Here is what i have done up to now.
Encryption method
public void encodeAndSaveFile(File videoFile, String path) {
try {
final byte[] encrypt = new byte[(int) videoFile.length()];
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir(path, Context.MODE_PRIVATE);
File mypath = new File(directory, "en1");
Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(this), new SystemNativeCryptoLibrary());
if (!crypto.isAvailable()) {
return;
}
OutputStream fileStream = new BufferedOutputStream(
new FileOutputStream(mypath));
OutputStream outputStream = crypto.getCipherOutputStream(
fileStream, new Entity("Passwordd"));
outputStream.write(encrypt);
outputStream.close();
} catch (UnsupportedOperationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(WebViewActivity.this,"Encrypted",Toast.LENGTH_LONG).show();
}
Decryption method
private void decodeFile(String filename,String path) {
Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(this),
new SystemNativeCryptoLibrary());
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir(path, Context.MODE_PRIVATE);
File file = new File(directory, filename);
try {
FileInputStream fileStream = new FileInputStream(file);
InputStream inputStream = crypto.getCipherInputStream(fileStream,
new Entity("Password"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
int read;
byte[] buffer = new byte[1024];
while ((read = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(WebViewActivity.this,"Decrypted",Toast.LENGTH_LONG).show();
}
But this code throwing following error at the run time
java.lang.illegalArgumentException: File contains a path separator
Then i have changed "mypath: variable of encodeAndSaveFile to this
File mypath = new File(directory, "en1");
and "file" variable of decodeFile to this
File file = new File(directory, filename);
Then no errors but. Encryption is not happening. Please help to solve this or suggest correct method for video encryption with conceal lib.
I have 3 sounds in the raw folder sound1.mp3 sound.mp3, sound3.mp3 as could be saved in a single file on the sdcard in android java
sound1 + sound2 + sound3 ---> soundnew
byte[] bitmapdata = baf.toByteArray();
File file = new File(Environment.getExternalStorageDirectory(), music1.mp3);
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write(bitmapdata );
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// handle exception
} catch (IOException e) {
// handle exception
}
I have been trying to save the file ( soundtrack ) from sdcard to the internal storage.
I have already done so with the image with this code
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/fairyTale/app_data/ImgMsc
File directory = cw.getDir("ImgMsc", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,Name+".png");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Now this works well, creates a directory and saves my bitmap, but i want to do the same thing for the music file.
Also i want to know how to read that file ( use it in the app ).
I have used this code to read the bitmap
File f=new File(path,name+".png");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
return b;
P.S. the code i have tried, but doesn't seem to be producing the right copy:
String filePath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+name;
File music = new File(filePath);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/fairyTale/app_data/ImgMsc
File directory = cw.getDir("ImgMsc", Context.MODE_PRIVATE);
File mypath=new File(directory,name+".mp3");
try {
InputStream is=new FileInputStream(music);
OutputStream os;
os = new FileOutputStream(mypath);
byte[] buff=new byte[1024];
int len;
while((len=is.read(buff))>0){
os.write(buff,0,len);
}
is.close();
os.close();
}
Here is the code that worked for me:
private void saveToInternalStorage(String name){
String filePath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+name;
File music = new File(filePath);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to your data
File directory = cw.getDir("ImgMsc", Context.MODE_PRIVATE);
File mypath=new File(directory,name+".mp3");
// Create imageDir
try {
InputStream is=new FileInputStream(music);
OutputStream os;
os = new FileOutputStream(mypath);
byte[] buff=new byte[1024];
int len;
while((len=is.read(buff))>0){
os.write(buff,0,len);
}
is.close();
os.close();
DeleteFile(filePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Hope it helps!
I have .3gp audio file which is stored in SD Card.I want to copy that file into another folder of sd card.I have googled a lot about it but didn't get any working idea.Please help me if anyone knows.The code I have tried till now is given below:
private void save(File file_save) {
String file_path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/RecordedAudio";
File file_dir = new File(file_path);
if (file_dir.exists()) {
file_dir.delete();
}
file_dir.mkdirs();
File file_audio = new File(file_dir, "audio"
+ System.currentTimeMillis() + ".3gp");
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(file_save);
out.close();
FileOutputStream fos = new FileOutputStream(file_audio);
byte[] buffer = bos.toByteArray();
fos.write(buffer);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is always create a new file with the size of 100.Thanks in advance...
The code when i call this save() method is:
mFileFirst = new File(mFileName);//mFileName is the path of sd card where .3gp file is located
save(mFileFirst);
Try this
private void save(File file_save) {
String file_path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/RecordedAudio";
File file_dir = new File(file_path);
if (file_dir.exists()) {
file_dir.delete();
}
file_dir.mkdirs();
File file_audio = new File(file_dir, "audio"
+ System.currentTimeMillis() + ".3gp");
try {
FileInputStream fis = new FileInputStream(file_save);
FileOutputStream fos = new FileOutputStream(file_audio);
byte[] buf = new byte[1024];
int len;
int total = 0;
while ((len = fis.read(buf)) > 0) {
total += len;
fos.write(buf, 0, len);
// Flush the stream once every so often
if (total > (20 * 1024)) {
fos.flush();
}
}
fos.flush();
fis.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}