how update Android MediaStore after i move a JPG file - android

my app move .jpg file to others folders and to get viewable in the stock gallery i have sendBroadcast ACTION_MEDIA_MOUNTED
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" +
Environment.getExternalStorageDirectory() )));
but this take much time.. i have understand (maybe) that i have to update manually with cursor/contentResolver in mediaStore directly to get this faster. can anyone help me on this? thanks..
my code actually is:
Uri uri = (Uri) list.get(cont);
Cursor cursor = managedQuery(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String app = cursor.getString(column_index);
File orig = new File( app.toString());
File dest = new File( destination_path +"/"+ orig.getName().toString());
orig.renameTo(dest);
with this i move a file from a path to another one.
after this, to get images in gallery
i have to sendBroadcast ACTION_MEDIA_MOUNTED

I just had to do the same, use following to update the MediaStore:
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newPath);
boolean successMediaStore = context.getContentResolver().update(
MediaStore.<TYPE>.Media.EXTERNAL_CONTENT_URI, values,
MediaStore.MediaColumns.DATA + "='" + oldPath + "'", null) == 1;
Replace <TYPE> with the correct media store... (Images, Video, Audio...)

Use the MediaScannerConnection to update the OS:
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// code to execute when scanning is complete
}
});

Related

Save an image to Shared Storage. Problem if the image exists with the same name

I want to save an image to Shared Storage, for Android 10 or high, and tell a user file name.
ContentResolver cr = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put( MediaStore.MediaColumns.DISPLAY_NAME, file_name );
contentValues.put( MediaStore.MediaColumns.MIME_TYPE, "image/png");
contentValues.put( MediaStore.MediaColumns.RELATIVE_PATH,
Environment.DIRECTORY_PICTURES+"/"+context.getString( R.string.app_name ));
uri = cr.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
out = cr.openOutputStream(uri);
// and write a bitmap by compress method
It is works fine. But if the image exists with the same name, for example "image.png" then system will create file name "image (1).png", "image (2).png" etc.
How to get real file name?
And one more. How to get image file size for information. I use now this, but not sure that is the right practice.
AssetFileDescriptor afd = context.getContentResolver().openAssetFileDescriptor(uri , "r");
info_out.size = afd.getLength();
afd.close();
Use cursor to get file name and check if it exist or not.Check the below code:
Cursor returnCursor =
getContentResolver().query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
do{
String path=cursor.getString(nameIndex);
File file=new File(path);
if(file.exists())
{
//your logic here
}
}while(returnCursor.moveToFirst());

MediaStore show Images after deleted from File Manager

I use MediaStore to get all Images from Android device. Then after I delete some of the Images from File Manager. Followed I use MediaStore again to get Images, and I get all deleted files which is problem.
Why MediaStore returns files that are no longer are on the device(Deleted from device) ?
Code which I am using to retrieve image from MediaStore.
Uri uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
Cursor cursor = activity.getContentResolver().query(uri, projection, null, null, MediaStore.MediaColumns.DATE_ADDED + " DESC");
String ImagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
Help to resolve this issue.
Why MediaStore returns files that are no longer are on the device(Deleted from device) ?
Because whatever file manager you used did not do anything to inform the MediaStore about the deletion of the files. MediaStore will find out eventually, but it may be several hours.
You can rescan Media using below code
MediaScannerConnection.scanFile(context, new String[]{imagePath}, null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
//something that you want to do
}
});

MediaStore show files after deleted

I use MediaStore to get all video files from Android device. Then I delete some of these videos. Followed I use MediaStore again, and I get all deleted files.
Why MediaStore returns files that are no longer are on the device?
Delete File:
File file = new File(filePath);
file.delete();
Get all video files from device:
public static List<String> getVideoFiles(Context context) {
String[] projection = { MediaStore.Video.Media.DATA };
Cursor cursor = context.getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null,
null, null);
List<String> videoList = new ArrayList<String>();
while (cursor.moveToNext()) {
videoList.add(cursor.getString(0));
}
Log.i(Constants.LOG_TAG, "get video files, load: " + videoList.size() + " "
+ videoList.toString());
return videoList;
}
MediaStore updates the list of media is not in real time. It needed time to test the relevance of its database. Try to make a call MediaStore after some time.
Or report manually about updating content.

how to obtain picture in camera with MediaStore

I try to use MediaStore to get picutures that stored in SDcard:DCIM/Camera.my code is like this:
Cursor track = mContext.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
but I get all pictures that in SDcards,I need get only in DCIM/Camera,help me please.
Try following code.
String SD_CARD_CAM_DIR = Environment.getExternalStorageDirectory() + File.separator + "DCIM" + File.separator + "Camera";
File file =new File(SD_CARD_CAM_DIR);
Cursor track = mContext.getContentResolver().query(Uri.fromFile(file), null, null,null,null);

Converting file:// scheme to content:// scheme

I am running to the problem with using Droid X's Files app and Astro file manager to select an image file. This two apps return the selected image with the scheme "file://" while Gallery returns the image with the scheme "content://". How do I convert the first schema to the second. Or how do I decode the image with the second format?
You probably want to convert content:// to file://
For gallery images, try something like this:
Uri myFileUri;
Cursor cursor = context.getContentResolver().query(uri,new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
if(cursor.moveToFirst())
{
myFileUri = Uri.parse(cursor.getString(0)).getPath();
}
cursor.close
Here, the problem is that, for all files we can't have a content Uri (content://). Because content uri is for those files which are part of MediaStore. Eg: images, audio & video.
However, for supported files, we can find its absolute path. Like for images as follows-
File myimageFile = new File(path);
Uri content_uri=getImageContentUri(this,myimageFile);
The generic method is as follows.
public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}}
Use ContentResolver.openInputStream() or related methods to access the byte stream. You shouldn't generally be worrying about whether it is a file: or content: URI.
http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.Uri)

Categories

Resources