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
}
Related
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();
}
}
I have a android app where I have a bitmap and I want to save it to the application data folder. The file is there after execution, but its 0kb and no picture is inside.
Where is the bug?
Here is my code:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(projDir + File.separator + newPath);
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
add fo.flush()
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.flush()
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
Try to add fo.flush()
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.flush()
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
Edit
try this:
File f = new File(projDir + File.separator + newPath);
FileOutputStream out = new FileOutputStream(f);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 40, out);
out.flush();
out.close();
Try it with FileOutputStream:
try {
FileOutputStream fos= new FileOutputStream(projDir + File.separator + newPath);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 40, fos);
} catch (Exception e) {
}
There is no need to call createNewFile() , it will be automatically created if it does not exist. I guess since you never delete it is already there and it is not created because of that.
Also as a good habit you should put clean up related code inside the finally block. This way if some error happens somewhere the file will be eventually closed.
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
boolean success = myBitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
if(!success) {
Log.w("myApp", "cannot compress image");
}
String patg = projDir + File.separator + newPath
File f = new File(projDir + File.separator + newPath);
Log.w("myApp", "cannot compress image");
try {
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fo != null) {
fo.flush();
fo.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
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
}
I wrote this code to save my canvas as bitmap but it didn't work. Can anyone help?
public void saveImage(){
try {
Bitmap bitmap = object.getDrawingCache();
path = Environment.getDataDirectory().getAbsolutePath();
file = new File( path.toString() +"/image.png");
FileOutputStream fos ;
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
isFileCreated = file.exists();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
Without knowing what you mean by "it didn't work", try creating the File differently. This is better than manually trying to build the path:
file = new File(Environment.getDataDirectory(), "image.png");