2014-08-29 89 views
0

我將圖像保存到SD卡中,但未顯示在手機的圖庫中。我可以在文件夾中看到保存的圖像,但該文件夾不在庫中。在Android的圖庫中如何顯示已保存的SD卡圖像?

我的代碼在這裏,如何修復它?

img_icon.setOnLongClickListener(new View.OnLongClickListener() { 

     @Override 
     public boolean onLongClick(View v) { 

      img_resim.buildDrawingCache(); 
      Bitmap bm = img_resim.getDrawingCache(); 

      OutputStream fOut = null; 

      Uri outputFileUri; 
      try { 
       root = new File(Environment.getExternalStorageDirectory() 
         + File.separator + "folder_name" + File.separator); 
       root.mkdirs(); 
       File sdImageMainDirectory = new File(root, "myPicName.jpg"); 
       outputFileUri = Uri.fromFile(sdImageMainDirectory); 
       fOut = new FileOutputStream(sdImageMainDirectory); 
      } catch (Exception e) { 
       Toast.makeText(context.getActivity(), 
         "Error occured. Please try again later.", 
         Toast.LENGTH_SHORT).show(); 
      } 

      try { 
       bm.compress(Bitmap.CompressFormat.PNG, 100, fOut); 
       fOut.flush(); 
       fOut.close(); 
      } catch (Exception e) { 

      } 

      context.getActivity() 
        .sendBroadcast(
          new Intent(
            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
            Uri.parse("file://" 
              + Environment 
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)))); 

      return true; 
     } 
    }); 

    return rowView; 
} 

感謝您的幫助..

+0

正確關閉手機圖庫,然後再次啓動該手機圖庫應用程序。 – Jamil 2014-08-29 15:14:43

回答

1

你的問題就在這裏:

context.getActivity() 
       .sendBroadcast(
         new Intent(
           Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, 
           Uri.parse("file://" 
             + Environment 
               .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)))); 

你問的Android重新建立索引的所有文件在Environment.DIRECTORY_PICTURES

要求掃描整個目錄樹是很浪費的。在你的情況下,它更浪費,因爲你沒有將你的文件寫入目錄。相反,你是該文件寫入到:

root = new File(Environment.getExternalStorageDirectory() 
        + File.separator + "folder_name" + File.separator); 
root.mkdirs(); 
File sdImageMainDirectory = new File(root, "myPicName.jpg"); 

因此,您的掃描不會拿起這個文件,你寫在外部存儲一些隨機點。

您需要決定存儲文件的適當位置,然後索引即一個文件。

相關問題