我想添加拍摄的照片到MediaStore,以便画廊应用程序可以找到他们(不重新启动设备)。应用程序的min是9。任何帮助,博客或文档感谢。
发布于 2014-02-17 07:17:15
在大多数设备上,您所需要做的就是等待一小段时间,新照片将被自动检测到。
如果您想要预先对图库进行刷新,您需要使用MediaScanner类,它将刷新图库-删除已删除的照片,添加新的照片等等……
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);
}希望这能帮上忙!
发布于 2014-02-17 07:17:15
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));在“保存”代码之后插入这一行代码。
这将触发媒体扫描,所有文件夹中的所有媒体文件(“.nomedia”文件除外)都将被更新&在图片库中可见。
来源。
MediaScanner文档。
或
// 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的示例代码。
发布于 2014-02-17 07:16:42
好的,这是我的代码,它为我工作,它给我所有的图片,我可以看到,在安卓画廊,只需调用这个函数从这一行
getallimages(Environment.getExternalStorageDirectory());我的职能如下
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);
}
}
}https://stackoverflow.com/questions/21822759
复制相似问题