File f = new File(MainActivity.appContext.getFilesDir(), "Captured.jpg");
f.delete();
//Create new file
FileOutputStream fos = MainActivity.appContext.openFileOutput("Captured.jpg", Context
.MODE_WORLD_WRITEABLE);
//FileOutputStream out = new FileOutputStream(file);
fos.close();
//Get reference to the file
filezz = new File(MainActivity.appContext.getFilesDir(), "Captured.jpg");
as MODE_WORLD_WRITEABLE is deprecated and i don't want to go with that code so
can i get the replcement of above code ?
Replace Context.MODE_WORLD_WRITEABLE with 0 or Context.MODE_PRIVATE.
If you are doing this from bitmap then use below code
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), "Captured.jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Related
I am saving a Bitmap (bitmap) to a file as follows:
String fileName = "image.jpg";
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName,
Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
How to access the saved image as a file? Thanks.
Try this.
String fileName = "image.jpg";
try {
String path = Environment.getExternalStorageDirectory().toString(); //path where the file will be stored.
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(path,fileName); // the File to save , append
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
don't forget to add Storage Permission and get the permission on runtime as well.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I m not able to convert the bitmap into string and pass it to the JSON by send the intent.putExtra.....And on receiving it on Create method of next activity....
The Default is giving error and i have jpeg images ...
How to fix this problem....
Intent Intent inn=getIntent()
bitmap = (Bitmap) inn.getParcelableExtra("bmp_img");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
public String imageCompression(String image) {
// to compress image
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
Bitmap b = BitmapFactory.decodeFile(image, bmpFactoryOptions);
Bitmap out = Bitmap.createScaledBitmap(b, 300, 300, false);
String[] bits = image.split("/");
String lastOne = bits[bits.length - 1];
File imageFile = null;
File imagesFolder = new File(Environment.getExternalStorageDirectory(),
"CompressedImages");
if (imagesFolder.isDirectory()) {
imageFile = new File(imagesFolder, lastOne);
FileOutputStream fOut;
try {
fOut = new FileOutputStream(imageFile);
out.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
fOut.flush();
fOut.close();
b.recycle();
out.recycle();
} catch (Exception e) {
e.printStackTrace();
}
} else {
imagesFolder.mkdirs();
imageFile = new File(imagesFolder, lastOne);
FileOutputStream fOut;
try {
fOut = new FileOutputStream(imageFile);
out.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
b.recycle();
out.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
File file = new File(imageFile.getPath());
byte imageData[] = new byte[(int) file.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e3) {
e3.printStackTrace();
}
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(imageData, 0, imageData.length);
} catch (IOException e1) {
e1.printStackTrace();
}
imageDataString = Base64.encodeToString(imageData, 0);
return imageDataString;
}
Use this methord and for intent passing pass just as String ......
i assume u are navigate from TO Activity A and B in sort of A->B
in A
Uri imageUri = Uri.parse("android.resource://your.package/drawable/fileName");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(intent)
and in B
Intent intent = this.getIntent();
(Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
have imagview want to save it to memory here is my code :
View content = findViewById(R.id.full_image_view);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
root.createNewFile();
FileOutputStream ostream = new FileOutputStream(root);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
after saving nothing happens and no image is exist ?
Try this..
Change root.createNewFile(); to cachePath.createNewFile();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
EDIT:
FileOutputStream ostream = new FileOutputStream(cachePath);
Use this function to save in SD card:
private void SaveIamge(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and add in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1 - you need an appropriate permission in amnifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2- out.flush() check the out is not null..
3 -
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/yourforlderName";
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, "yourforlderName" + image + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
if you don't create directory in your sdcard then how to store images in sdcard of specific location?
so please check this...i hope its useful to you.
To get bitmap from imageview:
imageview.buildDrawingCache();
Bitmap bm=imageview.getDrawingCache();
To save it in a file:
OutputStream fOut = null;
Uri outputFileUri;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
}
add in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
How about using following snippet ?
Drawable d = imageView.getBackground();
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
File file = new File(Environment.getExternalStorageDirectory(), "fileName.ext");
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
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
}