2016-03-06 63 views
4

我正在开发Android上的图像操作应用程序,但我一直在编写图像保存代码。即使拥有用户权限,也无法保存外部存储上的文件[Android]

这是我使用的方法:

private void saveImageToExternalStorage(Bitmap finalBitmap) { 
    String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); 
    File myDir = new File(root + "/saved_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); 
    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(); 
    } 


    // Tell the media scanner about the new file so that it is 
    // immediately available to the user. 
    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); 
       } 
      }); 

} 

我每次执行此,FileOutputStream中给出了一个FileNotFoundException异常。

我的清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.gabrielerestuccia.testimmagini"> 

<application 
    ... 
</application> 

<uses-feature android:name="android.hardware.camera" /> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

</manifest> 

我开发这个在Android 1.5.1工作室在Linux上,使用内置虚拟的Nexus 5采用Android 6.0。

感谢您的帮助。

+0

https://stackoverflow.com/questions/32635704/cant-get-the-permission – CommonsWare

+0

'每次我执行这个时,FileOutputStream都会给出一个FileNotFoundException'。但你仍在呼叫媒体扫描仪。更好:回归声明。 – greenapps

+0

'myDir.mkdirs();'。你应该检查返回值,如果为false则不能继续。如果目录已经存在,不要调用mkdirs。 – greenapps

回答

5

我在Android 6上有同样的问题。这是因为新的权限模型。

可以动态检查,如果您已获准使用下面的代码段:转至设置

private static boolean canWriteToExternalStorage(Context context) { 
    return ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; 
} 

您也可以使你的应用程序写入的权限 - >应用程序 - >您的应用程序 - >应用程序权限和打开存储项目。

为了让用户对这个权限,你应该在你的活动这一块的代码添加:

public void onCreate(Bundle savedInstanceState) { 
    ... 
    checkWritingPermission(); 
} 

@Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    if (requestCode == REQUEST_CODE_PERMISSION) { 
     if (grantResults.length >= 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
     // permission was granted 
     } else { 
     // permission wasn't granted 
     } 
    } 
    } 

    private void checkWritingPermission() { 
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
     // permission wasn't granted 
     } else { 
     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE_PERMISSION); 
     } 
    } 
    } 
+0

谢谢!我忘了棉花糖的新许可模式。 – gabrielication

+0

非常感谢你! – Darren

0
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     _bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 

     //you can create a new file name "test.jpg" in sdcard folder. 
     File f = new File(Environment.getExternalStorageDirectory() 
           + File.separator + "test.jpg") 
     f.createNewFile(); 
     //write the bytes in file 
     FileOutputStream fo = new FileOutputStream(f); 
     fo.write(bytes.toByteArray()); 

     // remember close de FileOutput 
     fo.close(); 
+1

'f.createNewFile();'。您可以删除该语句,因为该文件将由新的文件输出流创建。 – greenapps

相关问题