2014-10-27 58 views
0

我創建一個Android應用程序,需要一個屏幕截圖並保存在App圖像文件夾的時候,這是我用它來創建文件夾並保存截圖中的代碼:崩潰創建圖像文件夾

  String root = Environment.getExternalStorageDirectory().toString(); 
     File myDir = new File(root + "/Porte3D");  
     myDir.mkdirs(); 
     Random generator = new Random(); 
     int n = 10000; 
     n = generator.nextInt(n); 
     // Write your image name here 
     String fname = "Image-"+ n +".jpg"; 
     File file = new File (myDir, fname); 
     if (file.exists()) file.delete(); 
     try { 
       FileOutputStream out = new FileOutputStream(file); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
       out.flush(); 
       out.close(); 

     } catch (Exception e) { 
       e.printStackTrace(); 
     } 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 

我有2臺設備上測試這一點,三星Galaxy S2和摩托G,對S2的文件夾被創建和圖像被正確地存儲,但Moto G的,下面的錯誤崩潰:

10-27 09:09:41.422: A/libc(12069): Fatal signal 6 (SIGABRT) at 0x00002f25 (code=-6), thread 12435 (Thread-62263) 

有誰知道如何解決這個問題,以便在每個設備上工作?

+0

你必須確保ExternalStorage可使用它 – 2014-10-27 08:16:16

+0

這不是錯誤之前,該ExternalStorage可以用我的Moto G的 – Signo 2014-10-27 08:24:52

+0

一件事sendBroadca st(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse(「file://」+ Environment.getExternalStorageDirectory()))); 這是棄用從KitKat,雖然它不是你崩潰的原因,我剛剛出題,粘貼年完整的崩潰日誌 – 2014-10-27 08:27:00

回答

1

首先你必須檢查目錄的存在,然後創建它。在你的代碼替換

myDir.mkdirs(); 

通過

if (! myDir.exists()){ 
     if (! myDir.mkdirs()){ 
      Log.d("MyAPp", "failed to create directory"); 
      return null; 
     } 
    } 

的另一個問題是,在奇巧您的代碼將無法正常工作(這就是爲什麼它不是在Moto G的工作)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    // File f = new File("folderPATH", "fileName"); 
    Uri contentUri = Uri.fromFile(myDir); 
    mediaScanIntent.setData(contentUri); 
    sendBroadcast(mediaScanIntent); 
} 
else { 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" +Environment.getExternalStorageDirectory() + "/" + "FOLDER_TO_REFRESH"))); 
    } 

而且您的完整代碼將如下所示

String root = Environment.getExternalStorageDirectory().toString(); 
    File myDir = new File(root + "/Porte3D");  
    if (! myDir.exists()){ 
     if (! myDir.mkdirs()){ 
     Log.d("MyAPp", "failed to create directory"); 
     return null; 
     } 
    } 
    Random generator = new Random(); 
    int n = 10000; 
    n = generator.nextInt(n); 
    // Write your image name here 
    String fname = "Image-"+ n +".jpg"; 
    File file = new File (myDir, fname); 
    if (file.exists()) file.delete(); 
    try { 
      FileOutputStream out = new FileOutputStream(file); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      out.flush(); 
      out.close(); 

    } catch (Exception e) { 
      e.printStackTrace(); 
    } 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
     File f = new File(myDir, fname); 
     Uri contentUri = Uri.fromFile(f); 
     mediaScanIntent.setData(contentUri); 
     sendBroadcast(mediaScanIntent); 
    } 
    else { 
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
    } 
+0

使用此代碼應用程序不會崩潰,但在Moto G上我無法看到文件夾上的屏幕截圖,我沒有一個變量來傳遞FolderPath和FileName,我應該創建它們嗎? – Signo 2014-10-27 08:51:20

+0

@Signo編輯我的代碼。試着讓我知道如果你得到進一步的問題 – Praveen 2014-10-27 08:54:54

+0

沒有仍然畫廊不顯示..非常感謝幫助無論如何:) – Signo 2014-10-27 09:10:00

0

我不知道wheather其實際問題與您的代碼,但它值得一試

MediaScannerConnection.scanFile(this, 
      new String[] { file.toString() }, null, 
      new MediaScannerConnection.OnScanCompletedListener() { 
     public void onScanCompleted(String path, Uri uri) { 
      Log.i("ExternalStorage", "Scanned " + path + ":"); 
      Log.i("ExternalStorage", "-> uri=" + uri); 
     } 
}); 

請確保您有正確的權限讀取和寫入外部儲存 並傳遞要掃描

文件名
0

相信這會幫助ü男人

public class FileCache { 

    /** The cache dir. */ 
    private File cacheDir; 

    /** 
    * Instantiates a new file cache. 
    * 
    * @param context the context 
    */ 
    public FileCache(Context context){ 
     //Find the dir to save cached images 
     if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
      cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"TempImages"); 
     else 
      cacheDir=context.getCacheDir(); 
     if(!cacheDir.exists()) 
      cacheDir.mkdirs(); 
    } 

    /** 
    * Gets the file. 
    * 
    * @param url the url 
    * @return the file 
    */ 
    public File getFile(String url){ 
     String filename=String.valueOf(url.hashCode()); 
     File f = new File(cacheDir, filename); 
     return f; 

    } 

    /** 
    * Clear. 
    */ 
    public void clear(){ 
     File[] files=cacheDir.listFiles(); 
     if(files==null) 
      return; 
     for(File f:files) 
      f.delete(); 
    } 

}