Android 保存的图像在库中显示需要几分钟
我正在SD卡中保存一个位图,然后我立即调用了Android 保存的图像在库中显示需要几分钟,android,Android,我正在SD卡中保存一个位图,然后我立即调用了Intent.ACTION\u MEDIA\u MOUNTED 它会显示在多媒体资料中,但图像显示大约需要5分钟。有没有办法让它瞬间发生 File newFile = new File(newFilename); if (newFile.exists()) { newFile.delete(); } try { FileOutputStream out = new FileOutputStream(newFile); bitma
Intent.ACTION\u MEDIA\u MOUNTED
它会显示在多媒体资料中,但图像显示大约需要5分钟。有没有办法让它瞬间发生
File newFile = new File(newFilename);
if (newFile.exists()) {
newFile.delete();
}
try {
FileOutputStream out = new FileOutputStream(newFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
Toast.makeText(context, "Photo will appear in the Gallery in a few minutes.", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
启动ACTION_MEDIA_MOUNTED intent扫描整个SD卡,这可能就是您看到如此延迟的原因。请看MediaScannerConnection.scan。使用MediaScannerConnection,您可以添加单个文件,并在添加文件时收到通知。您需要将文件传递到MediaScannerConnection:。
这样,系统可以立即知道有一个新文件需要添加到库中。
现在,您必须等待系统扫描文件系统并查看您的文件,这当然不是即时的。
顺便说一句,当您实现此功能时,您应该能够删除Intent.ACTION\u MEDIA\u挂载调用,它不是用来广播文件添加的(MediaScannerConnection是)
We can use this way also
protected void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
Log.v(TAG + ".onActivityResult", "onActivityResult");
if (resultData != null) {
Uri selectedImage = resultData.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
uploadImagePath = cursor.getString(columnIndex);
bitmapUploadImage = BitmapFactory.decodeFile(uploadImagePath);
cursor.close();