FileNotFoundException异常一边捡从画廊图像 [英] FileNotFoundException while picking images from gallery
问题描述
我捡的图像形成画廊,但下面code段创建FileNotFoundException异常。 code是不错,但我不知道为什么它会引发异常。在此先感谢
下面是logcat的消息:
无法去code流:java.io.FileNotFoundException:/storage/emulated/0/Pictures/Screenshots/Screenshot_2015-09-15-17-27-49.png:打开失败:EACCES(权限被拒绝)
下面是我的code
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据);
如果(要求code == 0010安培;&安培;结果code == RESULT_OK&放大器;&安培;!数据= NULL)
{
乌里selectedImage = data.getData();
的String [] = filePathColumn {MediaStore.Images.Media.DATA}; 光标光标= getContentResolver()查询(selectedImage,filePathColumn,NULL,NULL,NULL);
cursor.moveToFirst(); INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
字符串imgDecodableString = cursor.getString(参数:columnIndex);
cursor.close(); 测试=(ImageView的)findViewById(R.id.testing);
//字符串解码后设置在ImageView的图像
Testing.setImageBitmap(BitmapFactory.de codeFILE(imgDecodableString)); }其他{
Utility.message(EditProfileActivity.this,你没有摄取的图像);
}
}
更好地利用下面的类将返回从那里你永远chosen.your code将无法有时例如文件path.doesn't事如果从谷歌你选择开车,你可能无法得到上述code.check路径了..
进口android.annotation.TargetApi;
进口android.content.ContentUris;
进口android.content.Context;
进口android.database.Cursor;
进口android.net.Uri;
进口android.os.Build;
进口android.os.Environment;
进口android.provider.DocumentsContract;
进口android.provider.MediaStore;
公共类FetchPath {
/ **
*从一个URI的文件路径。这将让存储访问的路径
*框架的文件,以及为MediaStore的_data字段和
*其他基于文件的ContentProviders。
*
* @参数语境的上下文。
* @参数URI的URI查询。
* @author paulburke
* /
@TargetApi(Build.VERSION_ codeS.KITKAT)
公共静态字符串的getPath(最终上下文的背景下,最终乌里URI){ 最终布尔isKitKat = Build.VERSION.SDK_INT> = Build.VERSION_ codeS.KITKAT; // DocumentProvider
如果(isKitKat&放大器;&放大器; DocumentsContract.isDocumentUri(上下文,URI)){
// ExternalStorageProvider
如果(isExternalStorageDocument(URI)){
最后弦乐的docId = DocumentsContract.getDocumentId(URI);
最终的String [] =拆分docId.split(:);
最终字符串类型=拆分[0]; 如果(主.equalsIgnoreCase(类型)){
返回Environment.getExternalStorageDirectory()+/+割裂[1];
} // TODO处理非主卷
}
// DownloadsProvider
否则如果(isDownloadsDocument(URI)){ 最后弦乐ID = DocumentsContract.getDocumentId(URI);
最后乌里contentUri = ContentUris.withAppendedId(
Uri.parse(内容://下载/ public_downloads),Long.valueOf(ID)); 返回getDataColumn(背景下,contentUri,NULL,NULL);
}
// MediaProvider
否则如果(isMediaDocument(URI)){
最后弦乐的docId = DocumentsContract.getDocumentId(URI);
最终的String [] =拆分docId.split(:);
最终字符串类型=拆分[0]; 乌里contentUri = NULL;
如果(形象.equals(类型)){
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}否则如果(视频.equals(类型)){
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}否则如果(音频.equals(类型)){
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
} 最后弦乐选择=_id =?;
最终的String [] = selectionArgs两个新的String [] {
分裂[1]
}; 返回getDataColumn(背景下,contentUri,选择selectionArgs两个);
}
}
// MediaStore(和一般)
否则,如果(内容.equalsIgnoreCase(uri.getScheme())){ //返回远程地址
如果(isGooglePhotosUri(URI))
返回uri.getLastPathSegment(); 返回getDataColumn(背景下,URI,NULL,NULL);
}
//文件
否则,如果(文件.equalsIgnoreCase(uri.getScheme())){
返回uri.getPath();
} 返回null;
} / **
*获取此URI数据列的值。这是有用
* MediaStore尤里斯,和其它基于文件的ContentProviders。
*
* @参数语境的上下文。
* @参数URI的URI查询。
*在查询中使用@参数选择(可选)过滤器。
*在查询中使用@参数selectionArgs两个(可选)选择参数。
* @返回_data列,它通常是一个文件路径的值。
* /
公共静态字符串getDataColumn(上下文的背景下,开放的URI,字符串的选择,
的String [] selectionArgs两个){ 光标光标= NULL;
最后String列=_data;
最终的String [] =投影{
柱
}; 尝试{
光标= context.getContentResolver()查询(URI,投影,选择,selectionArgs两个,
空值);
如果(光标=空&放大器;!&放大器; cursor.moveToFirst()){
最终诠释指数= cursor.getColumnIndexOrThrow(列);
返回cursor.getString(索引);
}
} {最后
如果(指针!= NULL)
cursor.close();
}
返回null;
}
/ **
* @参数URI的URI进行检查。
* @返回是否开放的权威ExternalStorageProvider。
* /
公共静态布尔isExternalStorageDocument(URI URI){
返回com.android.externalstorage.documents.equals(uri.getAuthority());
} / **
* @参数URI的URI进行检查。
* @返回是否开放的权威DownloadsProvider。
* /
公共静态布尔isDownloadsDocument(URI URI){
返回com.android.providers.downloads.documents.equals(uri.getAuthority());
} / **
* @参数URI的URI进行检查。
* @返回是否开放的权威MediaProvider。
* /
公共静态布尔isMediaDocument(URI URI){
返回com.android.providers.media.documents.equals(uri.getAuthority());
} / **
* @参数URI的URI进行检查。
* @返回是否乌里权威是谷歌的照片。
* /
公共静态布尔isGooglePhotosUri(URI URI){
返回com.google.android.apps.photos.content.equals(uri.getAuthority());
}}
用法
乌里photoUri = data.getData();
如果(photoUri!= NULL){
字符串文件路径= FetchPath.getPath(这一点,photoUri);
}
画廊意图
意图photoPickerIntent =新意图(Intent.ACTION_PICK);
photoPickerIntent.setType(图像/ *);
startActivityForResult(photoPickerIntent,请求code);
I am picking images form Gallery but the following code snippet create the FileNotFoundException. Code is fine but I don't know why it raises the exception. Thanks in advance
Here is the Logcat Message:
Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/Screenshots/Screenshot_2015-09-15-17-27-49.png: open failed: EACCES (Permission denied)
Below is my code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0010 && resultCode== RESULT_OK && data!=null)
{
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imgDecodableString = cursor.getString(columnIndex);
cursor.close();
Testing = (ImageView) findViewById(R.id.testing);
// Set the Image in ImageView after decoding the String
Testing.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
} else {
Utility.message(EditProfileActivity.this, "You've not picked image");
}
}
Better use the following class which will return the file path.doesn't matter from where ever you chosen.your code will fail some times for example if your choosing from google drive you might not get the path with the above code.check out..
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
public class FetchPath {
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @author paulburke
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
}
Usage
Uri photoUri = data.getData();
if (photoUri != null) {
String filePath = FetchPath.getPath(this, photoUri);
}
Gallery Intent
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, requestCode);
这篇关于FileNotFoundException异常一边捡从画廊图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!