android - 在 Nexus 5 (Android 5.1.1) 上从图库中选取图像返回 null,在其他设备上工作

标签 android android-intent gallery onactivityresult nexus-5

我有一个应用程序,它具有允许用户从图库中选择图像的功能。这是执行此操作的代码:

@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);
    }
}

获取路径方法:

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) 上测试它,它会返回空指针异常。

注意

我尝试过不同的应用程序:我认为使用 Google 相册从图库中获取图片可能会导致问题,但我将其安装在我的 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)

你能帮我解决这个问题吗?

提前致谢,干杯!

最佳答案

A Uri is not a file 。您无法可靠地获取用户通过 ACTION_GET_CONTENT 选择的内容的本地路径。您需要将 Uri 视为 Web 服务器的 URL,在该服务器上打开 Uri 上的流,无论是直接 (getContentResolver().openInputStream())或间接(通过毕加索)。

我不知道ImageResizer是什么,但如果它只适用于文件,请停止使用它。删除您的 getPath() 方法并仅使用 Uri

关于android - 在 Nexus 5 (Android 5.1.1) 上从图库中选取图像返回 null,在其他设备上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30647979/

相关文章:

android - 包含包含并按包含位置排序的字符串的过滤列表

android - 启动和绑定(bind)的服务何时销毁?

java - 切换多个按钮可见性

java - 如何绘制比设备分辨率更大的 GLSurfaceView 分辨率?

android - avd 模拟器是否支持 Google Play 商店?

android - Activity 泄露了原本绑定(bind)在这里的 ServiceConnection com.google.android.youtube.player.internal.r$e@42201a30

android - 是否可以编写一个 for 循环来将监听器分配给许多具有相同功能但使用最终整数的按钮?

c# - Windows 手机 8 : Load png from gallery

javascript - jQuery 媒体浏览器

android - 图库中的图像未显示