简体   繁体   中英

Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/57054 (has extras) }}

I'm writing an Image splitting method for a jigsaw puzzle game. Beside the default image the app can pick and split an image from the gallery. I get this error after choosing an image from the gallery and the app force close. Here are my code:

ImageView image;
Uri selectedImage;
private final int RESULT_LOAD_IMAGE = 1;
int chunkNumbers = 16;
ArrayList<Bitmap> chunkedImages;
Button[] buttons;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);
    choose = (Button) findViewById(R.id.ImgPicker);
choose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pickImageFromGallery();
        }
    });
}

void pickImageFromGallery() {

    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(pickPhoto, RESULT_LOAD_IMAGE);
}

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

    if(resultCode!=Activity.RESULT_CANCELED) {
        switch (requestCode) {

            case RESULT_LOAD_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    //  takenPictureData = handleResultFromChooser(data);
                    selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();
                    image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                    splitImage(image, chunkNumbers);

                /*if(selectedImage!=null) {
                    try {
                        InputStream picturePath = getContentResolver().openInputStream(selectedImage);
                        Bitmap bm = BitmapFactory.decodeStream(picturePath);



                        // Function of split the image(divide the image into pieces)
                        splitImage(image, chunkNumbers);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }*/
                }
                    //  ImageView imageView = (ImageView) findViewById(R.id.imgView);
                break;
        }
    }
    }
private void splitImage(ImageView image, int chunkNumbers) {

    //For the number of rows and columns of the grid to be displayed
    int rows,cols;

    //For height and width of the small image chunks
    int chunkHeight,chunkWidth;

    //To store all the small image chunks in bitmap format in this list
    chunkedImages = new ArrayList<Bitmap>(chunkNumbers);

    //Getting the scaled bitmap of the source image
    BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
    Bitmap bitmap = drawable.getBitmap();

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);

    rows = cols = (int) Math.sqrt(chunkNumbers);
    chunkHeight = bitmap.getHeight()/rows;
    chunkWidth = bitmap.getWidth()/cols;


    //xCoord and yCoord are the pixel positions of the image chunks
    int yCoord = 0;
    for(int x=0; x<rows; x++){
        int xCoord = 0;
        for(int y=0; y<cols; y++){
            chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
            xCoord += chunkWidth;
        }
        yCoord += chunkHeight;
    }

    BitmapDrawable[] bmd = (BitmapDrawable[]) chunkedImages.toArray();

    for(int i=0;i<15;i++)
    {
        buttons[i].setBackgroundDrawable(bmd[i]);
    }

}

The error is on line

image.setImageBitmap(BitmapFactory.decodeFile(picturePath));

Can anyone help with the above code? Already tried adding condition like if(resultCode != RESULT_CANCELED) or if (data != null) but still no result. Any help is appreciated. Thanks in advance.

Here my logcat:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/57054 (has extras) }} to activity {com.example.duc.puzzledemo/com.example.duc.puzzledemo.Game}: java.lang.NullPointerException
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3433)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3476)
        at android.app.ActivityThread.access$1200(ActivityThread.java:157)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5317)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.duc.puzzledemo.Game.onActivityResult(Game.java:350)
        at android.app.Activity.dispatchActivityResult(Activity.java:5515)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3429)

at android.app.ActivityThread.handleSendResult(ActivityThread.java:3476)
            at android.app.ActivityThread.access$1200(ActivityThread.java:157)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1337)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5317)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)

I changed my code to :

try {
                            InputStream picturePath = getContentResolver().openInputStream(selectedImage);
                            Bitmap bm = BitmapFactory.decodeStream(picturePath);


image.setImageBitmap(bm);
                            // Function of split the image(divide the image into pieces)
                            splitImage(image, chunkNumbers);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();

But the problem still exist.

You have a few problems here, not the least of which is that you asked a question about a crash without providing a full stack trace.

What is probably causing your crash is your invalid code trying to derive picturePath . A Uri is not a file . Treat the Uri the same way that you would a URL to a Web server.

Beyond that, you are doing I/O (reading in the image) and significant processing of that image (decoding it) on the main application thread, which freezes your UI while that work is being done.

There are many image loading libraries for Android , such as Universal Image Loader and Picasso. Most, if not all, of them can take your Uri and asychronously load the bitmap into your ImageView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question "Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} Failure delivering result ResultInfo{who=null, request=129742, result=-1, data=Intent { (has extras) }} to activity onActivityResult() and: failure delivering result resultinfo who=null request=1 result=-1 data=intent (has extras) Failure delivering result ResultInfo{who_null,request=1,result=-1, data =Intent{( has extras )}} to activity;NullPointerException java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131074, result=-1, data=Intent { (has extras) }} to activity java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity; java.lang.NullPointerException Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup…}} Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup...}} Failure delivering result ResultInfo(who=null, request=1, result=0, data=null)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM