cursor.getString返回带有有效uri的null [英] cursor.getString returns null with a valid uri
问题描述
程序突然停止工作.
我有一个URI:"content://com.android.providers.media.documents/document/image%3A13
",它是图像的文件路径.
I have a URI: "content://com.android.providers.media.documents/document/image%3A13
", a file path to an image.
选择URI的路径是这样的:
The path for the URI is chosen like so:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
try {
// This bit here
Bitmap bitmap = getPath(data.getData());
Log.i("Bitmap", "Bmp: " + data.getData());
}catch (Exception e){
Log.e("Error", "Error with setting the image.");
e.printStackTrace();
}
}
}
因此,调用getPath()
,将数据作为URI放入(URI是正确的,日志显示该信息)
So, getPath()
is called, putting the data in as a URI (URI is correct, the log shows that)
在getPath()
中:
private Bitmap getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,null);
cursor.moveToFirst();
// it is this line here, it returns null for some reason.
String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
// Convert file path into bitmap image using below line.
Log.i("File Path", "File name: " + filePath); // this comes out as NULL in the logcat
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
filePathForUpload = filePath;
try {
ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
bitmap = rotateBitmap(bitmap, orientation);
}catch (Exception e){
Log.d("Error", "error with bitmap!");
e.printStackTrace();
}
return bitmap;
}
Logcat输出:
java.lang.IllegalArgumentException: filename cannot be null
at android.media.ExifInterface.<init>(ExifInterface.java:121)
at build.com.build.SubmitPicActivity.getPath(SubmitPicActivity.java:123)
at build.com.build.SubmitPicActivity.onActivityResult(SubmitPicActivity.java:93)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
第123行是:String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
第93行是:Bitmap bitmap = getPath(data.getData());
有什么建议吗?
推荐答案
A Uri
is not necessarily a File
. The code that you are using to try to get a File
for a Uri
was never reliable and will not work much going forward.
请使用ContentResolver
和类似openInputStream()
和getType()
的方法适当地使用Uri
.本质上,您使用Uri
的方式与访问Web服务器的URL的方式相同,并且出于相同的原因:ContentProvider
浮出水面的内容不需要来自普通的
Please consume the Uri
appropriately, using a ContentResolver
and methods like openInputStream()
and getType()
. In essence, you treat a Uri
the same way that you would a URL to a Web server, and for much the same reason: there is no requirement for the content surfaced by a ContentProvider
to be coming from an ordinary File
that your app can access.
这篇关于cursor.getString返回带有有效uri的null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!