0

我有一個應用程序包含使用ImageViewViewPager不同的圖像。我想通過ImageView將當前圖像保存在SD Storage中。但它總是成功地保存在Phone Storage中。 我想將圖像保存在SD Card中,並且保存的圖像不在Gallery中顯示爲什麼?請幫我在這個問題:圖像保存在手機存儲而不是SD卡,也不顯示在手機圖庫爲什麼?


public void SaveIamge() { 
     String root = Environment.getExternalStorageDirectory().toString(); 
     File myDir = new File(root + "/Cute_Baby_Images"); 
     myDir.mkdirs(); 
     Random generator = new Random(); 
     int n = 10000; 
     n = generator.nextInt(n); 
     String fname = "Image-"+ n +".jpg"; 
     File file = new File (myDir, fname); 
     int currentImagePos = viewPager.getCurrentItem(); 
     Drawable drawable = viewPager.getResources().getDrawable(images[currentImagePos]); 
     Bitmap finalBitmap = ((BitmapDrawable) drawable).getBitmap(); 

     if (file.exists()) file.delete(); 
     try { 
      FileOutputStream out = new FileOutputStream(file); 
      finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 

      out.flush(); 
      out.close(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     Toast.makeText(this, "Sucessfully Save Image", Toast.LENGTH_LONG).show(); 

    } 
+0

查看https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)來創建您的文件。 – Daivid

+0

請更新我的代碼,我不明白鏈接。 –

回答

0

Environment.getExternalDirectory()回報手機存儲空間。要獲得SD卡的位置,請Find an external SD card location

,使圖像出現在畫廊,你應該讓媒體掃描儀掃描使用MediaScannerConnection.scanFile() 檢查文件Image, saved to sdcard, doesn't appear in Android's Gallery app

+0

請告訴我獲取外部存儲(SD卡)的功能是什麼? –

+0

看到這個答案:https://stackoverflow.com/a/15612964/5137352 –

+0

我不明白的鏈接..你可以修改我的代碼,我會非常感謝你。 –

0

創建一個名爲函數getDirectory()

private static File getDirectory(String variableName, String... paths) { 
    String path = System.getenv(variableName); 
    if (!TextUtils.isEmpty(path)) { 
     if (path.contains(":")) { 
      for (String _path : path.split(":")) { 
       File file = new File(_path); 
       if (file.exists()) { 
        return file; 
       } 
      } 
     } else { 
      File file = new File(path); 
      if (file.exists()) { 
       return file; 
      } 
     } 
    } 
    if (paths != null && paths.length > 0) { 
     for (String _path : paths) { 
      File file = new File(_path); 
      if (file.exists()) { 
       return file; 
      } 
     } 
    } 

    //If any there is no SECONDARY STORAGE are detected return INTERENAL STORAGE 
    return Environment.getExternalStorageDirectory(); 
} 

將代碼改爲

public void SaveIamge() { 

    String root = getDirectory("SECONDARY_STORAGE").getAbsolutePath(); 
    File myDir = new File(root + "/Cute_Baby_Images"); 
    myDir.mkdirs(); 
    Random generator = new Random(); 
    int n = 10000; 
    n = generator.nextInt(n); 
    String fname = "Image-"+ n +".jpg"; 
    File file = new File (myDir, fname); 
    int currentImagePos = viewPager.getCurrentItem(); 
    Drawable drawable = viewPager.getResources().getDrawable(images[currentImagePos]); 
    Bitmap finalBitmap = ((BitmapDrawable) drawable).getBitmap(); 

    if (file.exists()) file.delete(); 
    try { 
     FileOutputStream out = new FileOutputStream(file); 
     finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 

     out.flush(); 
     out.close(); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    Toast.makeText(this, "Sucessfully Save Image", Toast.LENGTH_LONG).show(); 
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + root))); 
} 

我有添加broadcast以通知MediaScanner重新掃描文件系統以查找新文件。這將解決在圖庫中未顯示圖像的問題。

+0

應用程序沒有變化。圖像再次保存在內部存儲器而不是外部存儲器中,請幫助我將圖像保存在外部存儲器中,您的代碼僅在圖庫中顯示圖像。 –

+0

@ AndroidLearner541粘貼您正在使用的權限 –

+0

@ AndroidLearner541檢查編輯的代碼對我來說是否正常工作 –

相關問題