Show the saved images in gallery - android

I am creating a directory to store the bitmap images.This is my code:
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/ABC");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
Log.i(TAG, "" + file);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
But this folder is not shown in gallery.I am using Android "Marshmallow" version.Can anyone help me?

Use this broadcast
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(out); //out is your file you saved/deleted/moved/copied
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
} else {
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}
refer this link : link to help

Related

How to save bitmap in custom gallery?

I have understood that I can create a folder in DCIM and if there is a file in it, the dir is displayed as an album name. I can create the dir just fine, in this case, I call the dir "ThoughtCast."
I try saving a PNG file, however, and it does not appear.
Here is my code:
public void savebitmap(Bitmap bmp) throws IOException {
String file_path =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "ThoughtCast/";
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, "sketchpad" + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
}
Any help will be appreciated. Thanks
Try this it may help you (it's in kotlin)
I am currently do this task
private fun saveImage(bitmap: Bitmap) {
var outStream: FileOutputStream? = null
// Write to SD Card
try {
val dir = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "ThoughtCast/")
dir.mkdirs()
val fileName = String.format("%s_%d.jpg", "Image", System.currentTimeMillis())
val outFile = File(dir, fileName)
outStream = FileOutputStream(outFile)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream)
outStream.flush()
outStream.close()
Utils.showSnackBar(binding.rootView, getString(R.string.image_saved))
} catch (e: FileNotFoundException) {
Crashlytics.logException(e)
} catch (e: IOException) {
Crashlytics.logException(e)
} finally {
}
}
Try This Code Below. It may work for You.
private void saveImageStorage(Bitmap finalBitmap) {
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DICM).toString();
File myDir = new File(root + "/" + <your folder if you want>);
if(!myDir.exists()){
myDir.mkdir();
}
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();
}
// inform to the media scanner about the new file so that it is immediately available to the user.
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}

Bitmap not saving in application folder in android nougat

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

how to save an image file in android?

I want save an image file in android, and the system gallery can view it or delete it. I use the following code, but found gallery could not find it,let alone view it or delete it. Any suggestion?
private void saveImage(Bitmap finalBitmap){
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + getString(R.string.folderName));
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".png";
File file = new File (myDir, fname);
if (file.exists ()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
} catch (Exception e) {
e.printStackTrace();
}
}
Just change this line :
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
To:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
if you want gallery to scan and add your app image folder then try this code after you save the image.
MediaScannerConnection.scanFile(this,new String[] { imagePath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
if(Config.LOG_DEBUG_ENABLED) {
// if required do something after the scan complet
Log.d(TAG, "scanned : " + path);
}
}

Sharing image failed

I'm trying to share an image but I don't know why I'm failing, could you help me please?
String imageUrl = web.get(position).getImage();
if (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))
imageUrl = "http://" + imageUrl;
Button button = (Button)rowView.findViewById(R.id.condividi);
final String finalImageUrl = imageUrl;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_TEXT, web.get(position).getTitle());
File file = writebitmaptofilefirst("the image", finalImageUrl );
Uri path = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM, path );
Intent send = Intent.createChooser(intent, null);
context.startActivity(send);
}
});
public static File writebitmaptofilefirst(String filename, String source) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extStorageDirectory + "/temp_images");
if (!mFolder.exists()) {
mFolder.mkdir();
}
OutputStream outStream = null;
File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, filename + ".jpg");
Log.e("file exist", "" + file + ",Bitmap= " + filename);
}
try {
URL url = new URL(source);
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("file", "" + file);
return file;
}
EDIT
String imageUrl = web.get(position).getImage();
if (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))
imageUrl = "http://" + imageUrl;
Button button = (Button)rowView.findViewById(R.id.condividi);
final String finalImageUrl = imageUrl;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_TEXT, web.get(position).getTitle());
String file = writebitmaptofilefirst("ndp_image", finalImageUrl);
//Uri path = Uri.fromFile(file);
intent.putExtra(Intent.EXTRA_STREAM, file );
Intent send = Intent.createChooser(intent, null);
context.startActivity(send);
}
});
return rowView;
}
public static String writebitmaptofilefirst(String filename, String source) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extStorageDirectory + "/temp_images/");
if (!mFolder.exists()) {
mFolder.mkdir();
}
OutputStream outStream = null;
File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, filename + ".jpg");
Log.e("file exist", "" + file + ",Bitmap= " + filename);
}
try {
URL url = new URL(source);
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("file", "" + file);
return file.getAbsolutePath();
}
Add permissions to your manifest
Remove space from file name (the image). With space you need decode the uri. You should paass the full path return file.getAbsolutePath(). You are just passing the file name.
In the case of file exists your not storing in the same path. You were not included the dictionary. extranlstoragepath+/temp_images/+the image.jpg try to log your file paths. And
File file = new File(mFolder.getAbsolutePath(), filename + ".jpg");
You have missed a / between two paramaters.
Wonderful blogpost about storing image

Questions reagarding bitmap image and image storage

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.

Categories

Resources