Search code examples
androidbitmapuriandroid-file

Cannot use Poly Picker multiple image picker library in Android


I am developing an Android app. In my app, I want to let user to choose multiple when user clicks upload button. So I used this library. I can successfully pop up dialog and choose multiple files. But the problem is when I convert URI of selected images to bitmap in onActivityResult, it is giving me error.

This is how I pop up picker in activity:

 private void getImages() {
        Intent intent = new Intent(GalleryActivity.this, ImagePickerActivity.class);
        nl.changer.polypicker.Config pickerConfig = new nl.changer.polypicker.Config(R.color.white,R.color.blue,10,R.color.green);
        ImagePickerActivity.setConfig(pickerConfig);
        startActivityForResult(intent, INTENT_REQUEST_GET_IMAGES);
    }

This is how I am converting to bitmap on result:

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

        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == INTENT_REQUEST_GET_IMAGES) {
                Parcelable[] parcelableUris = data.getParcelableArrayExtra(ImagePickerActivity.EXTRA_IMAGE_URIS);

                if (parcelableUris == null) {
                    return;
                }

                // Java doesn't allow array casting, this is a little hack
                Uri[] uris = new Uri[parcelableUris.length];
                System.arraycopy(parcelableUris, 0, uris, 0, parcelableUris.length);

                if (uris != null) {
                    bitmaps = new ArrayList<Bitmap>();
                    for (Uri uri : uris) {
                        try{
                            if(uri!=null)
                            {
                                Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                                bitmaps.add(bmp);
                            }
                        }
                        catch (IOException e)
                        {
                            Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }
                    if(bitmaps.size()>0)
                    {
                        confirmFileUpload();
                    }
                }
            }
        }
    }

As you can see above my code, it will reach to io exception block of try-catch statement.

This is the example of error toasted:

enter image description here

That kind of error throw whatever image I select. What is wrong with my code and how can I fix it?


Solution

  • Finally I found the solution. I problem was when I parse uri to string, the format is something like this:

       /sdcard/download/filename.png
    

    The uri string must be in this format:

       file:///sdcard/download/filename.png
    

    No Content Provider Found exception throws because my uri string does not have required prefix. So I convert the uri to string. Then added the prefix. Then I parse that string to URI back. Then it worked successfully.