Skip to content Skip to sidebar Skip to footer

Get Name And Full Path Of A File

I used Intent ACTION_GET_CONTENT to show recent files from phone memory. That includes images, pdf, google drive documents(pdf, xlsx) as shown in screenshot below. I want to get th

Solution 1:

I want to get the name and full path so that I can upload the file to server

There is no "full path", because there may not be a "file".

You are invoking ACTION_GET_CONTENT. This returns a Uri to some content. Where that content comes from is up to the developers of the ACTION_GET_CONTENT activity that the user chose. That could be:

  • An ordinary file on the filesystem that happens to be one that you could access
  • An ordinary file on the filesystem that resides somewhere that you cannot access, such as internal storage for the other app
  • A file that requires some sort of conversion for it to be useful, such as decryption
  • A BLOB column in a database
  • Content that is generated on the fly, the way this Web page is generated on the fly by the Stack Overflow servers
  • And so on

To use the content from the Uri, use a ContentResolver and openInputStream().

how to use this InputStream to upload to server API as file?

Either your chosen HTTP API supports an InputStream as the source of this content, or it does not. If it does, just use the InputStream directly. If not, use the InputStream to make a temporary copy of the content as a file that you can directly access (e.g., in getCacheDir()), upload that file, then delete the file when the upload is complete.

Solution 2:

try this

publicstatic ArrayList<String> getImagesFromCameraDir(Context context) {
        Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        ArrayList<String> filePaths = new ArrayList<>();
        finalString[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED};
        Cursor mCursor = context.getContentResolver().query(mImageUri, columns, MediaStore.Images.Media.DATA + " like ? ", newString[]{"%/DCIM/%"}, null);
        if (mCursor != null) {
            mCursor.moveToFirst();

            try {
                int uploadImage = 0;
                uploadImage = mCursor.getCount();
                for (int index = 0; index < uploadImage; index++) {
                    mCursor.moveToPosition(index);
                    int idx = mCursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    filePaths.add(mCursor.getString(idx));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                mCursor.close();
            }
        }
        return filePaths;
    }

Solution 3:

get real path from URI

publicStringgetRealPathFromURI(Uri contentUri) 
{
     String[] proj = { MediaStore.Audio.Media.DATA };
     Cursor cursor = managedQuery(contentUri, proj, null, null, null);
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
     cursor.moveToFirst();
     return cursor.getString(column_index);
}

FILENAME :

String filename = path.substring(path.lastIndexOf("/")+1);

Post a Comment for "Get Name And Full Path Of A File"