Hi I Am Trying to save my new bitmap image after applying filter into the SD-card or gallery. I am using this Code to save my bitmap that I have found on Stack overflow as well, but this code is not working for me
public void saveImage(View view) {
/*MediaStore.Images.Media.insertImage(getContentResolver(),newBitmap,"Image-Name","description");*/
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);
newBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(this,"Image Saved In SD Card",Toast.LENGTH_LONG).show();
}
Please let me know If I am doing any thing wrong?
Thanks in advance.
Make sure that you have added permission to write to sdcard
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Related
i am working on bitmap save to application folder in android
below nougat its work fine but in nougat i have issue so can anyone help me?
below my code for save bitmap
String getRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
File myDir = new File(getRoot + "/" + folderNAme);
String fname = picName + format;
File file = new File(myDir, fname);
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(form, quality, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
act.sendBroadcast(mediaScanIntent);
return file;
Thanks
Please try this:-
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
String fname = "filename.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();
}
}
You have to give runtime permissions for nougat for saving your file.
See this:
https://developer.android.com/about/versions/nougat/android-7.0-changes.html
I am using ACTION_IMAGE_CAPTURE to capture image using camera. It works fine, But the problem is that image is showing in imageview after clicking but can not saved in external or internal storage. Here is my code for saving image in external storage.
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DashBoard/");
file.mkdirs();
ticket = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DashBoard/Ticket.jpg";
file4 = new File(file, ticket);
try {
FileOutputStream out = new FileOutputStream(file4);
bitmap.compress(Bitmap.CompressFormat.JPEG , 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
ivTicket.setImageBitmap(bitmap);
Any solution ?
Use ImageWorker Library for easily saving bitmaps/drawables/base64.
How to Save
ImageWorker.to(context).
directory("ImageWorker").
subDirectory("SubDirectory").
setFileName("Image").
withExtension(Extension.PNG).
save(sourceBitmap,85)
Easy as that. Contributions are welcomed.
Follow it -
private void SaveImage(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 this in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
EDIT: By using this line you will be able to see saved images in the gallery view.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Reference
Try This:
private void SaveImage(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 this in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
another example here
UPDATE
instead of Environment.getExternalStorageDirectory() you can use your storage location as API level 30 and above Storage-policy is changed.
I am having an application containing different images using ImageView and ViewPager. I want to save the current image shown through ImageView in SD Storage. But it always saved in Phone Storage successfully.
I want to save the image in SD Card and also saved image not showing in Gallery Why? Kindly help me out in this issue:
public void SaveIamge() {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/Cute_Baby_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);
int currentImagePos = viewPager.getCurrentItem();
Drawable drawable = viewPager.getResources().getDrawable(images[currentImagePos]);
Bitmap finalBitmap = ((BitmapDrawable) drawable).getBitmap();
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();
}
Toast.makeText(this, "Sucessfully Save Image", Toast.LENGTH_LONG).show();
}
Environment.getExternalDirectory() returns phone storage. To get location of SD card, check Find an external SD card location
To make the images appear in gallery, you should let the media scanner scan the file using MediaScannerConnection.scanFile()
Check Image, saved to sdcard, doesn't appear in Android's Gallery app
Create a function called getDirectory()
private static File getDirectory(String variableName, String... paths) {
String path = System.getenv(variableName);
if (!TextUtils.isEmpty(path)) {
if (path.contains(":")) {
for (String _path : path.split(":")) {
File file = new File(_path);
if (file.exists()) {
return file;
}
}
} else {
File file = new File(path);
if (file.exists()) {
return file;
}
}
}
if (paths != null && paths.length > 0) {
for (String _path : paths) {
File file = new File(_path);
if (file.exists()) {
return file;
}
}
}
//If any there is no SECONDARY STORAGE are detected return INTERENAL STORAGE
return Environment.getExternalStorageDirectory();
}
Change your code to
public void SaveIamge() {
String root = getDirectory("SECONDARY_STORAGE").getAbsolutePath();
File myDir = new File(root + "/Cute_Baby_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);
int currentImagePos = viewPager.getCurrentItem();
Drawable drawable = viewPager.getResources().getDrawable(images[currentImagePos]);
Bitmap finalBitmap = ((BitmapDrawable) drawable).getBitmap();
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();
}
Toast.makeText(this, "Sucessfully Save Image", Toast.LENGTH_LONG).show();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + root)));
}
I have added a broadcast to notify the MediaScanner to re-scan the filesystem for new files. This will fix the problem of images are not showing in the gallery.
I am draw pie graph using achartEngine library .How to Pie graph convert as images and store in sdcard.
GraphicalView mChartview;
// after draw pie graph....
Bitmap bitmap =mChartview.toBitmap()
String fileName = "test" + ".png";
File file = new File(Environment.getExternalStorageDirectory(),
fileName);
FileOutputStream output = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, output);
But create 0 size image problem.
You can use following code for capturing,Here mChart is your GraphicalView
Bitmap bitmap1;
mChart.setDrawingCacheEnabled(true);
bitmap1 = Bitmap.createBitmap(mChart.getDrawingCache());
mChart.setDrawingCacheEnabled(false);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/DCIM/Camera");
myDir.mkdirs();
String fname = "Image123.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap1.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
I have the bitmap image that I got from my camera activity. Can someone please guide me as to how can I store this image in the gallery?
code:
In my button OnClickListener
Intent campic=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(campic,cameradata );
In my onActivityResult
if(resultCode==RESULT_OK)
{
Bundle bun=data.getExtras();
bmp=(Bitmap)bun.get("data");
SaveIamge(bmp);
iveventpic.setImageBitmap(bmp);
}
call this function to save bitmap in sdcard:
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();
}
}
By calling this line u have to store that image in the gallery:
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
and Add permission in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MediaStore.Images.Media.insertImage(getContentResolver(), bmp, title, desc);
As seen in this post.