android : deleting an image - android

I am deleting an image file from my application. I was doing
new File(filename).delete ();
This was actually deleting the file. But the image was still visible in the gallery.
On search i found that we should use
getContentResolver().delete(Uri.fromFile(file), null,null); to delete
But here i am getting the exception:
Unknown file URL. java.lang.IllegalArgumentException: Unknown URL
file:///mnt/sdcard/DCIM/Camera/IMG_20120523_122612.jpg
When i see with any file browser, this particular image is present. Please help me to fix this issue. Is there any other way to update gallery when image is physically deleted

Use the code below, it may help you.
File fdelete = new File(file_dj_path);
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" + file_dj_path);
} else {
System.out.println("file not Deleted :" + file_dj_path);
}
}
to refresh gallery after deleting image use below code for send Broadcast
(for < KITKAT API 14)
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
For >= KITKAT API 14 use below code
MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
/*
* (non-Javadoc)
* #see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
*/
public void onScanCompleted(String path, Uri uri)
{
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Because:
ACTION_MEDIA_MOUNTED
is deprecated in KITKAT(API 14).
EDITED 04-09-2015
its working fine check below code
public void deleteImage() {
String file_dj_path = Environment.getExternalStorageDirectory() + "/ECP_Screenshots/abc.jpg";
File fdelete = new File(file_dj_path);
if (fdelete.exists()) {
if (fdelete.delete()) {
Log.e("-->", "file Deleted :" + file_dj_path);
callBroadCast();
} else {
Log.e("-->", "file not Deleted :" + file_dj_path);
}
}
}
public void callBroadCast() {
if (Build.VERSION.SDK_INT >= 14) {
Log.e("-->", " >= 14");
MediaScannerConnection.scanFile(this, new String[]{Environment.getExternalStorageDirectory().toString()}, null, new MediaScannerConnection.OnScanCompletedListener() {
/*
* (non-Javadoc)
* #see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
*/
public void onScanCompleted(String path, Uri uri) {
Log.e("ExternalStorage", "Scanned " + path + ":");
Log.e("ExternalStorage", "-> uri=" + uri);
}
});
} else {
Log.e("-->", " < 14");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
}
below is logs
09-04 14:27:11.085 8290-8290/com.example.sampleforwear E/-->﹕ file Deleted :/storage/emulated/0/ECP_Screenshots/abc.jpg
09-04 14:27:11.085 8290-8290/com.example.sampleforwear E/-->﹕ >= 14
09-04 14:27:11.152 8290-8290/com.example.sampleforwear E/﹕ appName=com.example.sampleforwear, acAppName=/system/bin/surfaceflinger
09-04 14:27:11.152 8290-8290/com.example.sampleforwear E/﹕ 0
09-04 14:27:15.249 8290-8302/com.example.sampleforwear E/ExternalStorage﹕ Scanned /storage/emulated/0:
09-04 14:27:15.249 8290-8302/com.example.sampleforwear E/ExternalStorage﹕ -> uri=content://media/external/file/2416

I've seen a lot of answers suggesting the use of
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
This works but causes the Media Scanner to re-scan the media on the device. A more efficient approach would be to query/delete via the Media Store content provider:
// Set up the projection (we only need the ID)
String[] projection = { MediaStore.Images.Media._ID };
// Match on the file path
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[] { file.getAbsolutePath() };
// Query for the ID of the media matching the file path
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst()) {
// We found the ID. Deleting the item via the content provider will also remove the file
long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
contentResolver.delete(deleteUri, null, null);
} else {
// File not found in media store DB
}
c.close();

File file = new File(photoUri);
file.delete();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(photoUri))));
This code works for me and I think it better than remount whole SD card with Intent.ACTION_MEDIA_MOUNTED

To delete image,
ContentResolver contentResolver = getContentResolver();
contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.ImageColumns.DATA + "=?" , new String[]{ imagePath });

I tried all those solutions but had no luck in Android 6.
In the end, I found this snipped of code that worked fine.
public static void deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
String canonicalPath;
try {
canonicalPath = file.getCanonicalPath();
} catch (IOException e) {
canonicalPath = file.getAbsolutePath();
}
final Uri uri = MediaStore.Files.getContentUri("external");
final int result = contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath});
if (result == 0) {
final String absolutePath = file.getAbsolutePath();
if (!absolutePath.equals(canonicalPath)) {
contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
}
}
}
I also tested this in Android 4.4 and 5.1 and it works perfectly.

In Kotlin you can do this :
private fun deleteImage(path: String) {
val fDelete = File(path)
if (fDelete.exists()) {
if (fDelete.delete()) {
MediaScannerConnection.scanFile(this, arrayOf(Environment.getExternalStorageDirectory().toString()), null) { path, uri ->
Log.d("debug", "DONE")
}
}
}
}

sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
This code works, but it is very resource expensive. It unmounts & then mounts the SDCard which may affect some applications or take huge system resources in order to refresh the gallery. I am still looking for a best alternative & will post if i get one.

I had the same issue, and I tried three different methods to delete an image. Sometimes it was working sometimes it wasn't. After too much time spent now every method that I have will delete the image.What I wanna say is: BE CAREFUL WITH PROCESSING BITMAP. I was taking a picture persist it and then rotate if needed:
public static Bitmap rotatePictureToPortraitMode(String filePath, Bitmap myBitmap) {
try {
ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Log.d("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
} else if (orientation == 3) {
matrix.postRotate(180);
} else if (orientation == 8) {
matrix.postRotate(270);
}
myBitmap = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), matrix, true); // rotating bitmap
} catch (Exception e) {
}
return myBitmap;
}
after that I tried to delete the image but as I said previously it wasn't working. Removing this method helped to me to solve the issue.
Maybe this was only my issue but as soon as I removed this it helped me a lot, so I wanna say careful how you are processing the image. For my case I used the answer that is previously mentioned :
File file = new File(photoUri);
file.delete();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(new File(photoUri))));
Hope it helps!

public static boolean deltefolderwithimages(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deltefolderwithimages(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}

DocumentFile.fromSingleUri(context, uri).delete();
Working great for me

this all method deprecated for Android 11+,
for Delete any media file in Android11+ you can send request for delete.,
for this i found one dependency for this just check i hope your issue is resolve using this method.
https://github.com/jcredking/Delete1

Related

Unable to set picture as. "No appps can perform this action"

I am trying to give users an option to set image as wallpaper/whatsapp dp like this.
But I'm stuck with this code
Uri sendUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.a_day_without_thinking_mobile);
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(sendUri, "image/jpg");
intent.putExtra("mimeType", "image/jpg");
startActivity(Intent.createChooser(intent,"Set As"));
It shows a dialog that no apps can perform this action.
I also tried to check my Uri through this method
ContentResolver cr = getContentResolver();
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cur = cr.query(sendUri, projection, null, null, null);
if (cur != null) {
if (cur.moveToFirst()) {
String filePath = cur.getString(0);
if (new File(filePath).exists()) {
Log.d("URI: ","File path exist");
} else {
Log.d("URI: ","File not found");
}
} else {
Log.d("URI: ","URI ok but no enty found");
}
cur.close();
} else {
Log.d("URI: ","URI was invalid for some other reason");
}
And It always returned that the URI was invalid. But I'm sure that the image is valid jpg and is present in raw folder.
I tried changing URI paths but to no success.
Android file read format has been changed after targetSdkVersion >= 24
You can find the details here;
https://stackoverflow.com/a/38858040/1367450

How to add taken photo to MediaStore

I want to add taken photos to MediaStore so Gallery app can find them (without restarting device).
App's min sdk is 9. Any help, blog or documentation appreciated.
On most devices, all you need to do is wait a little while and the new photos will be detected automatically.
If you want to preform an immediate refresh to the gallery, you need to use the MediaScanner class, It will refresh the gallery - remove deleted photos, add new ones and so on...
public void refreshGallery() {
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
String newPhotoPath = "file:" + image.getAbsolutePath(); // image is the created file image
File file = new File(newPhotoPath);
Uri contentUri = Uri.fromFile(file);
scanIntent.setData(contentUri);
sendBroadcast(scanIntent);
}
Hope this helped!
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Insert this line of code after your 'save' code.
This will trigger a media scan and all media files in all folders (except with '.nomedia' files) will be updates & visible in gallery.
Source.
MediaScanner Documentation.
OR
// Tell 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);
}
});
Google's Sample Code.
ok its my code and it work for me it give all images which i can see in Android Gallery just call this function from this line
getallimages(Environment.getExternalStorageDirectory());
and my function is below
private void getallimages(File dir)
{
String[] STAR = { "*" };
final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
Cursor imagecursor = cntx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
if(new File(imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA))).length()<=10485760)
{
imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));
imageItem.id = id;
imageItem.selection = false; //newly added item will be selected by default
controller.images.add(imageItem);
}
}
}
You can ask the MediaScanner to scan a specific file, ie your image file on demand. This should produce less overhead than just asking the MediaScanner to scan everything for new files.
SO: how to run media scanner in android

Refresh the Gallery after deleting an image file?

I always found the following answer for my Question:
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
but it do not work on my System (Nexus4 Android 4. ...)
I can create a File and add it to the Media-DB whith this code
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
Where "file" is the new image-file i want to add.
after deleting the File I try to refresch the gallery by
Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
Uri contentUri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
intent.setData(contentUri);
context.sendBroadcast(intent);
or
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
but there are still empty placeholder in the Galery.
I do not know why?...
To be on the safe side I add too my Activity in the AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
but the result is the same. Any idea to solve the problem?
After the KitKat you can't send the Intent to run the MediaScanner on whole device's storage, because it is a CPU I\O intensive task and if every single app that download an image or delete one, call that intent battery would drain easily, hence they have decided to block that operation. Here are your options:
Use the old way for pre-KitKat
Pass your filePath:
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
} else{
MediaScannerConnection.scanFile(mContext, filePath, null, new MediaScannerConnection.OnScanCompletedListener() {
/*
* (non-Javadoc)
* #see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
*/
public void onScanCompleted(String path, Uri uri)
{
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
A more reliable approach is to update the MediaStore directly:
// Set up the projection (we only need the ID)
String[] projection = { MediaStore.Images.Media._ID };
// Match on the file path
String selection = MediaStore.Images.Media.DATA + " = ?";
String[] selectionArgs = new String[] { file.getAbsolutePath() };
// Query for the ID of the media matching the file path
Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
if (c.moveToFirst()) {
// We found the ID. Deleting the item via the content provider will also remove the file
long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
contentResolver.delete(deleteUri, null, null);
} else {
// File not found in media store DB
}
c.close();
Check below code snippet to verify all the cases for add/delete/move image file programmatically and intimate the gallery app to refresh the data
/***
* Refresh Gallery after add image file programmatically
* Refresh Gallery after move image file programmatically
* Refresh Gallery after delete image file programmatically
*
* #param fileUri : Image file path which add/move/delete from physical location
*/
public void refreshGallery(String fileUri) {
// Convert to file Object
File file = new File(fileUri);
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
// Write Kitkat version specific code for add entry to gallery database
// Check for file existence
if (file.exists()) {
// Add / Move File
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(new File(fileUri));
mediaScanIntent.setData(contentUri);
BaseApplication.appContext.sendBroadcast(mediaScanIntent);
} else {
// Delete File
try {
BaseApplication.appContext.getContentResolver().delete(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media.DATA + "='"
+ new File(fileUri).getPath() + "'", null);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
BaseApplication.appContext.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ getBaseFolder().getAbsolutePath())));
}
}
For Xamarin C# you can Use following Code!
Just pass the full File path to the Array
Android.Media.MediaScannerConnection.ScanFile(Android.App.Application.Context, new string[] { deletedImageFilePath}, null, null);

How can I refresh the Gallery after I inserted an Image in android?

I've added an inserted in Gallery using android API as following:
Images.Media.insertImage(ctx.getContentResolver(),
"scard/test.jpg", "Hello" ,
"description");
Actually the image that I passed its full path (scard/test.jpg) is already successfully inserted in the DB, but when you open the gallery you can't see it unless you switch off/on the device or Mount/Unmount the external memory.
It there any way to refresh the gallery on demand?
Thanks
Bassel Kh.
I encountered this problem recently, I tried #Samuh's solution, but not works perfectly until I found the solution from Google's example:
// Tell 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);
}
});
I tried myself and worked flawlessly.
Or, similarly, you might want to take a look at the reference of the MediaScanner Class and someone on StackOverflow asked this question before:
Image, saved to sdcard, doesn't appear in Android's Gallery app
The solution using the Media Scanner (sendBroadcast). But, probably, on sdcards with a lot of pics and data, this operation should reach a high processing cost.
There is another solution, after saving your media file on gallery, you should notify the gallery DB that another file was inserted. That can be done like that:
private void addImageGallery( File file ) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); // or image/png
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
This code doesn't save your media file to gallery, only saves the information about a new file in the gallery DB. You should store real file before.
This solution is faster because the gallery will not be fully re-scanned. But is not so trustful because all the information about the file was added manually.
static public boolean resetExternalStorageMedia(Context context) {
if (Environment.isExternalStorageEmulated())
return (false);
Uri uri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, uri);
context.sendBroadcast(intent);
return (true);
}
static public void notifyMediaScannerService(Context context, String path) {
MediaScannerConnection.scanFile(context,
new String[] { path }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
You can use this for refresh Android Gallery:
public void refreshAndroidGallery(Uri fileUri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(fileUri);
mContext.sendBroadcast(mediaScanIntent);
} else {
mContext.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
}
i tied everything then I found this perfect solution!
You can apply this method for any file type (jpg, png,pdf, etc)
public void refreshGallery(File f) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileUri = Uri.fromFile(f); //out is your output file
mediaScanIntent.setData(fileUri);
sendBroadcast(mediaScanIntent);
} else {
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
}
then
refreshGallery(newFileName);
newFIleName is a file path you can get your file path as
File newFileName = new File(filePath + "/" + fileName + ".jpg");

Image, saved to sdcard, doesn't appear in Android's Gallery app

I save an image to the sdcard and it doesn't appear in the Gallery application until I pull off the sdcard and return it back.
Do you have any idea why is it so?
Seems like the Gallery application has some cache that isn't updated on file save...
Actually, I also want to open the just-saved image in Gallery application and have no success with that this is my question about this issue.
A simpler solution is to use the static convenience method scanFile():
File imageFile = ...
MediaScannerConnection.scanFile(this, new String[] { imageFile.getPath() }, new String[] { "image/jpeg" }, null);
where this is your activity (or whatever context), the mime-type is only necessary if you are using non-standard file extensions and the null is for the optional callback (which we don't need for such a simple case).
My answer to the original question and to anyone else that may have this problem:
I was having this same problem, images in my app that people saved to the SD card were not showing up in their Gallery immediately. After some searching I found this one line of code inserted after my 'save to sdcard' code that fixed the problem:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
The system scans the SD card when it is mounted to find any new image (and other) files. If you are programmatically adding a file, then you can use this class:
http://developer.android.com/reference/android/media/MediaScannerConnection.html
You can also add an Image to the Media Gallery by intent, have a look at the example code to see how it is done:
ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, imageTitle);
image.put(Images.Media.DISPLAY_NAME, imageDisplayName);
image.put(Images.Media.DESCRIPTION, imageDescription);
image.put(Images.Media.DATE_ADDED, dateTaken);
image.put(Images.Media.DATE_TAKEN, dateTaken);
image.put(Images.Media.DATE_MODIFIED, dateTaken);
image.put(Images.Media.MIME_TYPE, "image/png");
image.put(Images.Media.ORIENTATION, 0);
File parent = imageFile.getParentFile();
String path = parent.toString().toLowerCase();
String name = parent.getName().toLowerCase();
image.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
image.put(Images.Media.SIZE, imageFile.length());
image.put(Images.Media.DATA, imageFile.getAbsolutePath());
Uri result = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
Gallery refresh including Android KITKAT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File("file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
else
{
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
Here is the code for the MediaScannerConnection:
MyMediaConnectorClient client = new MyMediaConnectorClient(newfile);
MediaScannerConnection scanner = new MediaScannerConnection(context, client);
client.setScanner(scanner);
scanner.connect();
newfile is the File object of your new/saved file.
there is an app in the emulator that says - ' Dev Tools'
click on that and select ' Media Scanning'.. all the images ll get scanned
Let your activity implement 'MediaScannerConnectionClient'
and add this to your activity:
private void startScan()
{
if(conn!=null) conn.disconnect();
conn = new MediaScannerConnection(YourActivity.this,YourActivity.this);
conn.connect();
}
#Override
public void onMediaScannerConnected() {
try{
conn.scanFile(yourImagePath, "image/*");
} catch (java.lang.IllegalStateException e){
}
}
#Override
public void onScanCompleted(String path, Uri uri) {
conn.disconnect();
}
this work with me
File file = ..... // Save file
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
File folderGIF = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/newgif2"); //path where gif will be stored
success = folderGIF.mkdir(); //make directory
String finalPath = folderGIF + "/test1.gif"; //path of file
.....
/* changes in gallery app if any changes in done*/
MediaScannerConnection.scanFile(this,
new String[]{finalPath}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Here I am sharing code that can load image in form of bitmap from and save that image on sdcard gallery in app name folder.
You should follow these steps
Download Image Bitmap first
private Bitmap loadBitmap(String url) {
try {
InputStream in = new java.net.URL(url).openStream();
return BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Please also provide following permission in your AndroidManifest.xml file.
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
Here is whole code that is written in Activty in which we want to perform this task.
void saveMyImage(String appName, String imageUrl, String imageName) {
Bitmap bmImg = loadBitmap(imageUrl);
File filename;
try {
String path1 = android.os.Environment.getExternalStorageDirectory()
.toString();
File file = new File(path1 + "/" + appName);
if (!file.exists())
file.mkdirs();
filename = new File(file.getAbsolutePath() + "/" + imageName
+ ".jpg");
FileOutputStream out = new FileOutputStream(filename);
bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, appName);
image.put(Images.Media.DISPLAY_NAME, imageName);
image.put(Images.Media.DESCRIPTION, "App Image");
image.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
image.put(Images.Media.MIME_TYPE, "image/jpg");
image.put(Images.Media.ORIENTATION, 0);
File parent = filename.getParentFile();
image.put(Images.ImageColumns.BUCKET_ID, parent.toString()
.toLowerCase().hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName()
.toLowerCase());
image.put(Images.Media.SIZE, filename.length());
image.put(Images.Media.DATA, filename.getAbsolutePath());
Uri result = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
Toast.makeText(getApplicationContext(),
"File is Saved in " + filename, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
Hope that it can solve your whole problem.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Does not seem to work on KITKAT. It throws permission denial exception and crashes the app.
So for this, I have done the following,
String path = mediaStorageDir.getPath() + File.separator
+ "IMG_Some_name.jpg";
CameraActivity.this.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri
.parse("file://" + path)));
Hope it helps.
Use this after saving the image
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
My code for MyMediaConnectorClient:
public class MyMediaConnectorClient implements MediaScannerConnectionClient {
String _fisier;
MediaScannerConnection MEDIA_SCANNER_CONNECTION;
public MyMediaConnectorClient(String nume) {
_fisier = nume;
}
public void setScanner(MediaScannerConnection msc){
MEDIA_SCANNER_CONNECTION = msc;
}
#Override
public void onMediaScannerConnected() {
MEDIA_SCANNER_CONNECTION.scanFile(_fisier, null);
}
#Override
public void onScanCompleted(String path, Uri uri) {
if(path.equals(_fisier))
MEDIA_SCANNER_CONNECTION.disconnect();
}
}
You need to give permissions to the Gallery app.
Just long press the gallery app icon in the home screen and tap on 'APP INFO' that pops up at the top of the screen. Doing it will show the gallery app settings. Now go in Permissions tab and enable the storage, camera permissions by toggling it.
Now go to your native gallery app and you will get the your saved images.
This will aslo solve your problem if your image in gallery are not showing instead they might showing a 404 type bitmap in midle.
please add a the tags that are in my code with your image because there must some meta data in order to show image in gallery.
String resultPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES)+
getString(R.string.directory) + System.currentTimeMillis() + ".jpg";
new File(resultPath).getParentFile().mkdir();
try {
OutputStream fileOutputStream = new FileOutputStream(resultPath);
savedBitmap.compress(CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e2) {
e2.printStackTrace();
}
savedBitmap.recycle();
File file = new File(resultPath);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Photo");
values.put(MediaStore.Images.Media.DESCRIPTION, "Edited");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis ());
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
values.put(MediaStore.Images.ImageColumns.BUCKET_ID, file.toString().toLowerCase(Locale.US).hashCode());
values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, file.getName().toLowerCase(Locale.US));
values.put("_data", resultPath);
ContentResolver cr = getContentResolver();
cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
return resultPath;
Try this one, it will broadcast about a new image created, so your image visible. inside a gallery.
photoFile replace with actual file path of the newly created image
private void galleryAddPicBroadCast() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(photoFile);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}

Categories

Resources