Search code examples
javaandroidfile-upload

What is the difference between getting multi image from gallery and files folder Android 9?


I use ;

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            if(requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK  && data != null) {
                if(imagePathList==null){
                    imagePathList = new ArrayList<>();
                }
                if(data.getData() != null) {
                    Uri imgUri = data.getData();
                    getImageFilePath(imgUri);
                } else if(data.getClipData() != null) {
                    int count = data.getClipData().getItemCount();
                    for (int i=0; i<count; i++) {
                        Uri imageUri = data.getClipData().getItemAt(i).getUri();
                        getImageFilePath(imageUri);
                    }
                }
            }
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                galleryAddPic();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

And;

public void getImageFilePath(Uri uri) {

        File file = new File(uri.getPath());
        String[] filePath = file.getPath().split(":");
        String imageId = filePath[filePath.length - 1];
        Cursor cursor = getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ", new String[]{imageId}, null);
        if (cursor != null) {
            cursor.moveToFirst();
            imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));// this is work for only get image from files folder
            if (!imagePathList.contains(imagePath)) {
                imagePathList.add(imagePath);
                addView(imagePath);
                buttonSendImages.setVisibility(View.VISIBLE);
            }
            cursor.close();
        }
    }

When ı use ;

imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

to get multi image from gallery folder ı got "android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0" error. But this is work fine when ı use getting image from files folder. How can ı fix this problem?


Solution

  • I change;

    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                    intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
                    startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE_MULTIPLE);
                    photoAlert.dismiss();
    

    I change open scanner for gallery ACTION_GET_CONTENT to ACTION_OPEN_DOCUMENT. It solve my problem.