vendredi 22 mai 2015

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.

Aucun commentaire:

Enregistrer un commentaire