我有一個應用程序,它具有允許用戶從圖庫中選擇圖像的功能。下面是這樣做的代碼:從Nexus 5(Android 5.1.1)上的圖庫中選取圖像返回null,在其他設備上工作
@OnClick(R.id.imageView_fragmentUploadLayout_uploadedArt)
public void setUploadedArtImageView() {
//startActivityForResult(new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY);
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), GET_FROM_GALLERY);
}
而且onActivityResult方法:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Detects request codes
if(requestCode == GET_FROM_GALLERY && resultCode == Activity.RESULT_OK && data != null) {
selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
file = new File(selectedImagePath);
//ParseFile parseImage = new ParseFile();
Picasso.with(getActivity())
.load(selectedImageUri.toString())
.resize(500, 500)
.centerCrop()
.noPlaceholder()
.into(uploadedArtImageView);
uploadedArtImageViewPlaceHolder.setVisibility(View.GONE);
ImageResizer.resize(file, 640, 480, ResizeMode.FIT_EXACT);
}
}
的getPath方法:
public String getPath(Uri uri) {
// just some safety built in
if(uri == null) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
if(cursor != null){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
如果我測試它在我的三星S2它的工作原理,(目前在Cyanogen Mod,Android 4.0.4上),但如果我在Nexus 5(Android 5.1.1)上測試它,它會返回空指針異常。
注意
我嘗試了多種不同的應用程序:我認爲,從與谷歌照片畫廊獲得的圖片可能引起的問題,但我安裝了它在我的S2和它的作品。
這裏是堆棧跟蹤:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131075, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:25147 flg=0x1 }} to activity {com.entu.artapp/com.entu.artapp.activity.FragmentManager}: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at java.io.File.fixSlashes(File.java:185)
at java.io.File.<init>(File.java:134)
at com.entu.artapp.fragments.UploadPictureFragment.onActivityResult(UploadPictureFragment.java:363)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
at android.app.Activity.dispatchActivityResult(Activity.java:6192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
你能幫我解決這個問題?
在此先感謝,歡呼!
檢查。 http://stackoverflow.com/a/30576188 – siriscac