2017-05-09 121 views
0

我通过作物意图在SD卡中保存了一个位图文件,我想在另一个活动中读取它。但是,我一直收到以下运行时错误:无法解码流:java.io.FileNotFoundException:file:/sdcard/oc.jpg:打开失败:ENOENT(没有这样的文件或目录) E/ReadFile:位图必须为非-null从SD卡读取位图

Uri uri=Uri.parse("file:///sdcard/oc.jpg"); 
//save output image in uri 
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 

读同一文件中的另一活动:

Bitmap image4 = BitmapFactory.decodeFile("file:///sdcard/oc.jpg"); 

我试图this solution但它没有工作,我得到了相同的运行时间错误。

+0

“我通过作物意图在SDcard中保存了一个位图文件” - [Android没有'CROP''Intent'](https://commonsware.com/blog/2013/01/23/no -Android-不 - 不具备的,作物intent.html)。 “无法解码流:java.io.FileNotFoundException:file:/sdcard/oc.jpg” - 不要硬编码路径(使用方法,像'Environment.getExternalStorageDirectory()'),并确保您有适当的权限(包括运行时权限)。 – CommonsWare

+0

我试过String imageInSD = Environment.getExternalStorageDirectory()。getAbsolutePath()+“oc.jpg”; image4 = BitmapFactory.decodeFile(imageInSD);但仍然无法读取位图,并且包含运行时权限 – MKH

+0

'.... getAbsolutePath()+“oc.jpg”'应该是'.getAbsolutePath()+“/ oc.jpg”' – greenapps

回答

0

你需要写外部存储,请确保您添加的权限:

<manifest ...> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
... 

检查,如果外部存储空间可用于读取和写入

public boolean isExternalStorageWritable() { 
    String state = Environment.getExternalStorageState(); 
    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     return true; 
    } 
    return false; 
} 

使用root的公共目录而不是使用Android的根目录。

如果你想保存在外部存储公用文件,使用getExternalStoragePublicDirectory()

public File getAlbumStorageDir(String albumName) { 
    // Get the directory for the user's public pictures directory. 
    File file = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_DCIM), albumName); 
    if (!file.mkdirs()) { 
     Log.e(LOG_TAG, "Directory not created"); 
    } 
    return file; 
} 

如果你想保存仅对您的应用程序文件,使用getExternalFilesDir()

public File getAlbumStorageDir(Context context, String albumName) { 
    // Get the directory for the app's private pictures directory. 
    File file = new File(context.getExternalFilesDir(
      Environment.DIRECTORY_DCIM), albumName); 
    if (!file.mkdirs()) { 
     Log.e(LOG_TAG, "Directory not created"); 
    } 
    return file; 
} 

,我忘了提:

decodeFile预计的路径