2014-10-27 56 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(); 
    } 

}