无法将结果ResultInfo

无法将结果ResultInfo

本文介绍了无法将结果ResultInfo {who = null,request = 2,result = -1,data = Intent {}}传递给活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法,可以让用户选择使用相机意图捕获照片然后上传到应用程序:

I have a method that gives users the option to use the camera intent to capture a photo to then upload to the application:

public void UploadImageToFeed() {

        CharSequence colors[] = new CharSequence[] {"Gallery", "Take a Picture"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Upload an Image");
        builder.setIcon(R.drawable.ic_upload_image);
        builder.setItems(colors, (dialog, which) -> {
            if (which == 0) {
                Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
                photoPickerIntent.setType("image/*");
                startActivityForResult(photoPickerIntent, SELECT_PHOTO);
            } else {
                Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File file = new File(Environment.getExternalStorageDirectory(), "camera.jpg");
                Uri uri = Uri.fromFile(file);
                captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                startActivityForResult(captureIntent, SELECT_PHOTO);
            }
        });
        builder.show();

    }

拍摄照相机图像时,我会在实际上传图像之前对其进行处理以减小其尺寸.这是我崩溃的地方.

When a camera image is taken, I process the image to reduce its size before it is actually uploaded. This is where I get a crash.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) {
            case SELECT_PHOTO:
                if (resultCode == RESULT_OK) {

                    ImageView imageToUpload = (ImageView) findViewById(R.id.imageToUpload);
                    imageToUpload.setVisibility(View.VISIBLE);

                    Uri selectedImage = imageReturnedIntent.getData();
                    InputStream imageStream = null;
                    try {
                        imageStream = getContentResolver().openInputStream(selectedImage);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                    int nh = (int) (yourSelectedImage.getHeight() * (512.0 / yourSelectedImage.getWidth()));
                    Bitmap scaled = Bitmap.createScaledBitmap(yourSelectedImage, 512, nh, true);
                    imageToUpload.setImageBitmap(scaled);

                }
        }
    }

例外:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/197149/ORIGINAL/NONE/724456777 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F197149/ORIGINAL/NONE/724456777} }} to activity {com.yitter.android/com.yitter.android.activity.YeetActivity}: java.lang.IllegalArgumentException: width and height must be > 0


at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
                                                                        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                                        at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                     Caused by: java.lang.IllegalArgumentException: width and height must be > 0
                                                                        at android.graphics.Bitmap.createBitmap(Bitmap.java:829)
                                                                        at android.graphics.Bitmap.createBitmap(Bitmap.java:808)
                                                                        at android.graphics.Bitmap.createBitmap(Bitmap.java:739)
                                                                        at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:615)
                                                                        at com.yitter.android.activity.YeetActivity$override.onActivityResult(YeetActivity.java:350)
                                                                        at com.yitter.android.activity.YeetActivity$override.access$dispatch(YeetActivity.java)
                                                                        at com.yitter.android.activity.YeetActivity.onActivityResult(YeetActivity.java:0)
                                                                        at android.app.Activity.dispatchActivityResult(Activity.java:6456)
                                                                        at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
                                                                        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                                        at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的第一个怀疑是该错误发生在我按比例缩小相机图像的地方,但是从我的堆栈跟踪来看并不太明显.当我删除负责重新缩放图像的两行代码时,出现以下错误:

My first suspicion was that the error occurs where I'm scaling the camera image down, but it's not so obvious from my stack trace. When I remove the two lines of code responsible for rescaling the image, I get the following error:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent {  }} to activity {com.yitter.android/com.yitter.android.activity.YeetActivity}: java.lang.NullPointerException: uri
                                                                      at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
                                                                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                                      at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                   Caused by: java.lang.NullPointerException: uri
                                                                      at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60)
                                                                      at android.content.ContentResolver.openInputStream(ContentResolver.java:645)
                                                                      at com.yitter.android.activity.YeetActivity.onActivityResult(YeetActivity.java:366)
                                                                      at android.app.Activity.dispatchActivityResult(Activity.java:6456)
                                                                      at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
                                                                      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                                      at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

出什么问题了?

推荐答案

ACTION_IMAGE_CAPTURE不返回Uri.您知道图像应该为Uri的位置-—您将其放在EXTRA_OUTPUT中.您需要保持该位置(包括通过保存的实例状态Bundle)并在那里查看.

ACTION_IMAGE_CAPTURE does not return a Uri. You know the Uri where the image is supposed to be — you put it in EXTRA_OUTPUT. You need to hold onto that location (including via the saved instance state Bundle) and look there.

还请注意,一旦将targetSdkVersion提升到25或更高版本,Uri.fromFile()将无法在Android 7.0+上运行.

Also note that Uri.fromFile() will not work on Android 7.0+, once you raise your targetSdkVersion to 25 or higher.

此示例应用演示了使用,包括使用FileProvider避免Uri.fromFile().

这篇关于无法将结果ResultInfo {who = null,request = 2,result = -1,data = Intent {}}传递给活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:10