2

我在 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();
}
4

3 回答 3

1

您需要将文件传递给 MediaScannerConnection:http: //developer.android.com/reference/android/media/MediaScannerConnection.html
这样系统可以立即知道有一个新文件需要添加到库中。
现在,您必须等到系统扫描文件系统并查看您的文件,这当然不是即时的。
顺便说一句,当你实现了这个,你应该能够删除 Intent.ACTION_MEDIA_MOUNTED 调用,它不是在这里广播文件添加(MediaScannerConnection 是)。

于 2013-05-13T18:42:43.213 回答
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();
于 2013-05-13T18:22:53.090 回答
0

触发 ACTION_MEDIA_MOUNTED 意图是扫描整个 SD 卡,这可能就是您看到这种延迟的原因。在此处查看 MediaScannerConnection.scan 。使用 MediaScannerConnection,您可以添加单个文件,并在添加时收到通知。

于 2013-05-13T18:37:24.470 回答