我在SD卡中保存了一个位图,我之后立即调用了Intent.ACTION_MEDIA_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();
}
答案 0 :(得分:1)
您需要将文件传递给MediaScannerConnection:http://developer.android.com/reference/android/media/MediaScannerConnection.html 这样,系统可以立即知道有一个新文件需要添加到图库中 现在,您必须等到系统扫描文件系统并查看您的文件,这当然不是即时的 顺便说一下,当你实现这个时,你应该能够删除Intent.ACTION_MEDIA_MOUNTED调用,这里不是广播文件添加(MediaScannerConnection是)。
答案 1 :(得分:0)
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();
答案 2 :(得分:0)
触发ACTION_MEDIA_MOUNTED意图是扫描整个SD卡,这可能就是你看到这种延迟的原因。看看MediaScannerConnection.scan here。使用MediaScannerConnection,您可以添加单个文件,并在添加后收到通知。