0

I know that there is a lot of examples on the internet for it but no one of those does what I really want. I want to select multiple images with an intent and get it "url" like this code does with one. (I know that this code cannot select more than one, but I use this as example of what I'm trying to do)

Code:

public static final int RESULT_LOAD_IMG = 0;
public void LoadImg() {
        Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }

ArrayList<Part> files = new ArrayList<>();

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

        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
            Uri 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);
            files.add(new FilePart("uploadedfile[" + String.valueOf(files.size()) + "]", new File(picturePath)));
            cursor.close();
        }
    }

Searching on the internet I found this example from Laith Mihyar in this post: Select multiple images from android gallery and I want to do the same here, but the file path is different here and dont work for what I'm trying to do.

Code:

int PICK_IMAGE_MULTIPLE = 1
 String imageEncoded;    
 List<String> imagesEncodedList;
Intent intent = new Intent();
  intent.setType("image/*");
 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
 intent.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE_MULTIPLE);
@Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            // When an Image is picked
            if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                String[] filePathColumn = { MediaStore.Images.Media.DATA };
                imagesEncodedList = new ArrayList<String>();
                if(data.getData()!=null){

                    Uri mImageUri=data.getData();

                    // Get the cursor
                    Cursor cursor = getContentResolver().query(mImageUri,
                            filePathColumn, null, null, null);
                    // Move to first row
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    imageEncoded  = cursor.getString(columnIndex);
                    cursor.close();

                }else {
                    if (data.getClipData() != null) {
                        ClipData mClipData = data.getClipData();
                        ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
                        for (int i = 0; i < mClipData.getItemCount(); i++) {

                            ClipData.Item item = mClipData.getItemAt(i);
                            Uri uri = item.getUri();
                            mArrayUri.add(uri);
                            // Get the cursor
                            Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
                            // Move to first row
                            cursor.moveToFirst();

                            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                            imageEncoded  = cursor.getString(columnIndex);
                            imagesEncodedList.add(imageEncoded);
                            cursor.close();

                        }
                        Log.v("LOG_TAG", "Selected Images" + mArrayUri.size());
                    }
                }
            } else {
                Toast.makeText(this, "You haven't picked Image",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                    .show();
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

Thanks

Community
  • 1
  • 1
PaulD
  • 37
  • 7
  • Why doesnt it work? And what are you trying to do? Pretty vague all. And what is the problem when a file path is different? Unclear. – greenapps Aug 22 '16 at 20:30
  • I need to get the path to a file, but when I try with the second one I get an app name (like com.appname.x) and a file with an id instead of a path that is what I'm trying to get. I try to get something like: "/content/files/image.png" and I get "com.company.appname/&file6647" or something like that – PaulD Aug 22 '16 at 20:38
  • 1
    For what do you need a path to a file? Use the uri! You can do everything with it. What do you want to do? – greenapps Aug 22 '16 at 20:41
  • The Uri return the rare url with the getPath() and I'm trying to upload those files with the Ion lib, that needs a path – PaulD Aug 22 '16 at 20:43
  • You better look for a small function that uploads file by uri. (30. lines?). Or use a library that manages uries. – greenapps Aug 22 '16 at 20:46
  • The problem is that the url that this code return is not valid(not at least using the uri) cause I can't even load it in an ImageView. – PaulD Aug 22 '16 at 20:47
  • The url ? I thought we were taking about uries. What do you mean? Show your code please. – greenapps Aug 22 '16 at 20:54

1 Answers1

0

There is no "file path". ACTION_GET_CONTENT does not have to return a file Uri, and most of the time on newer devices it will return a content Uri.

Use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. Use DocumentFile and fromSingleUri() to get at metadata about the content identified by the Uri.

I can't even load it in an ImageView

Picasso and other image-loading libraries (even Ion) work with content Uri values without an issue.

I'm trying to upload those files with the Ion lib, that needs a path

The best solution would be to use a better HTTP client library, one that offers greater flexibility. OkHttp (with Okio) should be able to handle uploading from a Uri.

Or, use ContentResolver and openInputStream() to get an InputStream on the content. Use FileOutputStream to get a stream on some local file (e.g., in getCacheDir()). Use Java I/O to copy the bytes from the InputStream to the FileOutputStream. Then, use the local file with Ion.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Could you help me with an example for loading the file with Ion pls? I'm not very acquainted with this and I really need to use Ion. – PaulD Aug 22 '16 at 21:04