2013-05-13 72 views
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(); 
} 

回答

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(); 
0

射擊的ACTION_MEDIA_MOUNTED意圖掃描整個SD卡這可能是爲什麼你看到這樣的延遲。看看MediaScannerConnection.scan here。使用MediaScannerConnection可以添加單個文件,並在添加完成後收到通知。

1

您需要將您的文件傳遞到MediaScannerConnection:http://developer.android.com/reference/android/media/MediaScannerConnection.html
這樣,系統可以立即知道需要將新文件添加到圖庫中。
現在,您必須等到系統掃描文件系統並查看您的文件,這當然不是即時的。
順便說一句,當你將實現這一點,你應該能夠刪除Intent.ACTION_MEDIA_MOUNTED調用,它不是在這裏廣播文件添加(MediaScannerConnection是)。

相關問題