FileNotFound异常而来自Android图库加载位图图像 [英] FileNotFound exception while loading bitmap images from Android gallery
问题描述
我想创建从我的Android库位图图像。
我这样做是在AsyncTask的,这就是为什么我在执行光标。在code是以下
{尝试
URI = Uri.parse(uriStr);
}赶上(例外五){
Log.e(LOG_TAG,与图像加载问题:+ E);
}
如果(URI == NULL)
继续;
位图BM = NULL;
尝试{
BM = MediaStore.Images.Media.getBitmap(activity.getContentResolver(),URI);
}赶上(FileNotFoundException异常五){
Log.e(LOG_TAG,位图没有发现:+ E);
}赶上(IOException异常五){
Log.e(LOG_TAG,位图IO错误:+ E);
当我调试,我注意到有抛出FileNotFoundException异常,
输出:
13 08-17:47:55.754 30333-30668 / com.example.jonas.shoppinglist
W / System.err的:java.io.FileNotFoundException:没有内容提供商:
/storage/emulated/0/Download/unnamed.jpg 08-17 13:47:55.833
30333-30668 / com.example.jonas.shoppinglist W / System.err的:在
android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1086)
08-17 13:47:55.921 30333-30668 / com.example.jonas.shoppinglist
W / System.err的:在
android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:941)
08-17 13:47:55.975 30333-30668 / com.example.jonas.shoppinglist
W / System.err的:在
android.content.ContentResolver.openInputStream(ContentResolver.java:666)
08-17 13:47:56.019 30333-30668 / com.example.jonas.shoppinglist
W / System.err的:在
android.provider.MediaStore $ $图片Media.getBitmap(MediaStore.java:849)
08-17 13:47:56.062 30333-30668 / com.example.jonas.shoppinglist
W / System.err的:在
com.example.jonas.shoppinglist.processes.ShoppingGallery.doInBackground(ShoppingGallery.java:51)
块引用>其结果是,我没有得到我的画廊之一单个位图图像。有什么问题?
解决方案你可以试试下面code从画廊achiving正确的图像路径.....
公共无效pickImage(){
意向意图=新意图(Intent.ACTION_GET_CONTENT);
intent.setType(图像/ *);
startActivityForResult(意向,1000);
} @覆盖
公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,结果code,数据);
如果(要求code == 1000安培;&安培;结果code == Activity.RESULT_OK){
如果(数据== NULL){
//显示错误
返回;
} 如果(Build.VERSION.SDK_INT&下; 11)
的ImagePath = RealPathUtil.getRealPathFromURI_BelowAPI11(这一点,
data.getData()); // SDK> = 11安培;&安培; SDK< 19
否则如果(Build.VERSION.SDK_INT所述19)
的ImagePath = RealPathUtil.getRealPathFromURI_API11to18(这一点,
data.getData()); // SDK> 19(Android 4.4系统)
其他
的ImagePath = RealPathUtil.getRealPathFromURI_API19(这一点,
data.getData()); }
}
-----------------------------------
//类
进口android.annotation.Sup pressLint;
进口android.content.Context;
进口android.content.CursorLoader;
进口android.database.Cursor;
进口android.net.Uri;
进口android.provider.DocumentsContract;
进口android.provider.MediaStore; 公共类RealPathUtil { @燮pressLint(NewApi)
公共静态字符串getRealPathFromURI_API19(上下文的背景下,开放的URI){
串文件路径=;
串wholeID = DocumentsContract.getDocumentId(URI); //分割在结肠,在阵列中使用的第二项
串ID = wholeID.split(:)[1]; 的String [] =列{} MediaStore.Images.Media.DATA; //其中id等于
字符串SEL = MediaStore.Images.Media._ID +=; 光标光标= context.getContentResolver()查询(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
列,SEL,新的String [] {ID},NULL); INT参数:columnIndex = cursor.getColumnIndex(列[0]); 如果(cursor.moveToFirst()){
文件路径= cursor.getString(参数:columnIndex);
}
cursor.close();
返回文件路径;
}
@燮pressLint(NewApi)
公共静态字符串getRealPathFromURI_API11to18(上下文的背景下,乌里contentUri){
的String [] = PROJ {MediaStore.Images.Media.DATA};
字符串结果= NULL; CursorLoader cursorLoader =新CursorLoader(
的背景下,
contentUri,PROJ,NULL,NULL,NULL);
光标光标= cursorLoader.loadInBackground(); 如果(指针!= NULL){
INT与Column_Index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
结果= cursor.getString(Column_Index中);
}
返回结果;
} 公共静态字符串getRealPathFromURI_BelowAPI11(上下文的背景下,乌里contentUri){
的String [] = PROJ {MediaStore.Images.Media.DATA};
光标光标= context.getContentResolver()查询(contentUri,凸出,NULL,NULL,NULL);
INT与Column_Index
= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(Column_Index中);
}
}I am trying to create a bitmap image from my Android gallery. I do this in an AsyncTask and this is the reason why I have a cursor in my implementation. The code is the following
try { uri = Uri.parse(uriStr); } catch (Exception e) { Log.e(LOG_TAG, "problem with the image loading: " + e); } if(uri == null) continue; Bitmap bm = null; try{ bm = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), uri); } catch (FileNotFoundException e) { Log.e(LOG_TAG, "Bitmap not found: " + e); } catch (IOException e) { Log.e(LOG_TAG, "Bitmap IO error: " + e);
When I debug, I notice there is a FileNotFoundException thrown, the output:
08-17 13:47:55.754 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ java.io.FileNotFoundException: No content provider: /storage/emulated/0/Download/unnamed.jpg 08-17 13:47:55.833 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1086) 08-17 13:47:55.921 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:941) 08-17 13:47:55.975 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ at android.content.ContentResolver.openInputStream(ContentResolver.java:666) 08-17 13:47:56.019 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:849) 08-17 13:47:56.062 30333-30668/com.example.jonas.shoppinglist W/System.err﹕ at com.example.jonas.shoppinglist.processes.ShoppingGallery.doInBackground(ShoppingGallery.java:51)
The result is that I didn't get one single bitmap image of my gallery. What is the problem?
解决方案you can try below code for achiving proper image path from gallery ..... public void pickImage() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, 1000); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1000 && resultCode == Activity.RESULT_OK) { if (data == null) { // Display an error return; } if (Build.VERSION.SDK_INT < 11) ImagePath = RealPathUtil.getRealPathFromURI_BelowAPI11(this, data.getData()); // SDK >= 11 && SDK < 19 else if (Build.VERSION.SDK_INT < 19) ImagePath = RealPathUtil.getRealPathFromURI_API11to18(this, data.getData()); // SDK > 19 (Android 4.4) else ImagePath = RealPathUtil.getRealPathFromURI_API19(this, data.getData()); } } ----------------------------------- //class import android.annotation.SuppressLint; import android.content.Context; import android.content.CursorLoader; import android.database.Cursor; import android.net.Uri; import android.provider.DocumentsContract; import android.provider.MediaStore; public class RealPathUtil { @SuppressLint("NewApi") public static String getRealPathFromURI_API19(Context context, Uri uri){ String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the array String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } @SuppressLint("NewApi") public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader( context, contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if(cursor != null){ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){ String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } }
这篇关于FileNotFound异常而来自Android图库加载位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!