1

I want to implement file chooser in android. I have implemented below code to open file chooser.

public static void showFileChooser(Activity activity, Fragment fragment) {
        try {

            File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "example");
            if (!imageStorageDir.exists()) {
                imageStorageDir.mkdirs();
            }
            File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
            mCapturedImageURI = Uri.fromFile(file); // save to the private variable

            final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
            captureIntent.putExtra("capturedimageuri", mCapturedImageURI.toString());

            // Intent for Audio Recording
            final Intent audioRecordIntent = new Intent();
            audioRecordIntent.setAction(IAdoddleConstants.ACTION_AUDIO_RECORD);

            final Intent videoRecordIntent = new Intent();
            videoRecordIntent.setAction(IAdoddleConstants.ACTION_VIDEO_RECORD);

            // Use the GET_CONTENT intent from the utility class
            Intent target = com.asite.adoddle.filechooser.FileUtils.createGetContentIntent();
            // Create the chooser Intent


            if (activity != null) {
                Intent intent = Intent.createChooser(
                        target, activity.getString(R.string.chooser_title));

                intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent,audioRecordIntent, videoRecordIntent});
                activity.startActivityForResult(intent, IMAGE_ANNOTATION_REQUEST_CODE);
            } else {
                Intent intent = Intent.createChooser(
                        target, fragment.getString(R.string.chooser_title));

                intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent,audioRecordIntent, videoRecordIntent});
                fragment.startActivityForResult(intent, IMAGE_ANNOTATION_REQUEST_CODE);
            }

        } catch (ActivityNotFoundException e) {
            AdoddleLog.e(DEBUG_TAG, "Error:" + e);
        } catch (Exception ex) {
            ex.printStackTrace();
            CommonUtilities.showToast(activity, activity.getString(R.string.error_message), Toast.LENGTH_LONG);
        }
    }

Now it opens filechooser same like below.

enter image description here

Now when I click on that "Documents" I get different apps from where I can choose files same like below.

enter image description here

Here you can see applications like Google Drive and Dropbox. Now I want to select files from this application. Now when I select file from dropbox then in onActivityResult(int requestCode, int resultCode, Intent data) I get content://com.dropbox.android.FileCache/filecache/2642b6eb-f9da-4775-b6c5-6cb4d6884018 uri from intent. Now How to get real path from this uri? I want to attach this file in my activity. Also I am not able to get real path of google drive's file.

Kush Patel
  • 1,228
  • 1
  • 13
  • 34
  • do not use a file path, use `InputStream` instead - see `ContentResolver` docs for more info – pskink Sep 04 '17 at 06:51
  • @pskink so you mean by using InputStream I write that file on my sd card and then use that sd card's file path. – Kush Patel Sep 04 '17 at 06:53
  • why do you need file path at all? cannot you just use `InputStream`? (or `ParcelFileDescriptor` / `FileDescriptor`)? – pskink Sep 04 '17 at 06:54
  • @pskink I have one method in ndk which read file from given file path and send it's content to server. So if i select any file from dropbox or google drive using this file chooser then that file also should be uploaded on server. – Kush Patel Sep 04 '17 at 06:59
  • then use `ParcelFileDescriptor`, again see `ContentResolver` docs for more info – pskink Sep 04 '17 at 07:01
  • @pskink Again using `ParcelFileDescriptor` , anyhow you have to read that file and copy it to sd card and then pass that sd card path. So I think for this type of files (which I selected from google drive, dropbox or similar apps) I have to write that file on sd card then use that sd card path for further usage. – Kush Patel Sep 04 '17 at 07:33
  • no, using `PFD` you can get `int` "file descriptor" that can be used in [fdopen](https://linux.die.net/man/3/fdopen) or passed directly to [read](https://linux.die.net/man/2/read) method – pskink Sep 04 '17 at 07:39
  • As pskink already said use InputStream is = getContentResolver().openInputStream(uri);. Then read from the stream and copy the bytes to a FileOutputStream. – greenapps Sep 04 '17 at 08:15
  • `I have one method in ndk which read file from given file path`. Better add a method that reads a file from an input stream. – greenapps Sep 04 '17 at 08:17
  • @greenapps `"Better add a method that reads a file from an input stream."` in ndk? by calling `CallIntMethod` in a loop? it would be damn slow... – pskink Sep 04 '17 at 08:20
  • Well i dont know. Its just an idea that came to mind. – greenapps Sep 04 '17 at 08:23
  • @pskink I use `getContentResolver().openInputStream()` method. – Kush Patel Sep 04 '17 at 10:48
  • By using inputstream I wrote that file on sdcard and then passing that sdk path to c++ component. – Kush Patel Sep 04 '17 at 10:51

1 Answers1

-1

To get Image or Video Real Path use :

protected String getPhotoUriRealPath(Uri contentURI) {
    String path = "";
    if (contentURI == null) {
        return path;
    }
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);

    if (cursor == null) {
        path = contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        path = cursor.getString(idx);

    }
    if (cursor != null) {
        cursor.close();
    }
    return path;
}

protected String getVideoUriRealPath(Uri contentURI) {
    String path = "";
    if (contentURI == null) {
        return path;
    }
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);

    if (cursor == null) {
        path = contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA);
        path = cursor.getString(idx);

    }
    if (cursor != null) {
        cursor.close();
    }
    return path;
}
Hossein Kurd
  • 3,184
  • 3
  • 41
  • 71