1

I am trying to create a personal chatting app which has a button. When click of that button the frame becomes visible which have buttons on click chooser is been created like this. When the image button is clicked it creates the chooser activity after selecting the file I have saved it in imagefile Uri.

But when I try to get the path of the file by using the data.getdata().getpath() method it gives doument/236 as the output but didn't give the actual path of the file. When I try to use fileutlis to get the path then it says "can't resolve fileutils". Please help me so that I can get the path of my file.

imagesend.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        checker = "image";
        Intent imageIntent = new Intent();
        imageIntent.setAction(Intent.ACTION_GET_CONTENT);
        imageIntent.setType("image/*");
        startActivityForResult(Intent.createChooser(imageIntent,"Select Image"),438);
    }
});

 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 438 && resultCode == RESULT_OK && data!=null && data.getData()!=null){
        loadingBar.setTitle("Sending Message");
        loadingBar.setMessage("Please wait...");
        loadingBar.setCanceledOnTouchOutside(false);
        loadingBar.show();

        imagefile = data.getData();

        String filepath = data.getData().getPath();

        if(checker.equals("pdf")){
            pdfFilemessage();
        }else if(checker.equals("image")){
            //imagefilemessage();
            Toast.makeText(personalChat.this,filepath,Toast.LENGTH_SHORT).show();
        }
    }
}
Furkan İlhan
  • 65
  • 3
  • 10
  • No you will not try to get a path to a classic file. Use data.getData().toString() or data.getData() directly to open an InputStream where you can read the contents of the file. Dont use things like getRealPathFromUri(). They might work for a time but if you want to prepaire for Android.Q then use the content scheme directly. Its not that difficult. – blackapps Oct 03 '19 at 12:45
  • Everyone who advertises getRealPathFromUri() should be abandoned here. – blackapps Oct 03 '19 at 12:52

3 Answers3

2

This line returns the Uri of the file.

imagefile = data.getData();

What you have to do is,

  public String getRealPathFromURI(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);
    }

Add this to your code.

String filePath = this.getRealPathFromURI(imagefile);

Refer this: https://stackoverflow.com/a/13209514

Malavan
  • 789
  • 7
  • 27
  • U can give this refrence link in question comment section.u did same code as per refrence link – Nikunj Patel Oct 03 '19 at 13:01
  • If I put in a comment. It won't be specific at all . – Malavan Oct 03 '19 at 13:05
  • thanks for the help... it works when I select a file from the gallery but when try to select the file from the file manager it gives a null value – Academy for Brilliance Oct 03 '19 at 13:39
  • MediaStore.Audio.Media.DATA and other that inherit from android.provider.MediaStore.MediaColumns.DATA that This constant was deprecated in API level 29. Apps may not have filesystem permissions to directly access this path. Instead of trying to open this path directly, apps should use ContentResolver#openFileDescriptor(Uri, String) to gain access. – Andrew Oct 16 '19 at 21:51
0

You can try this:

Uri fileUri = intent.getData();

Now from this you can use the below gist file to generate the file path from the URI

https://gist.github.com/tatocaster/32aad15f6e0c50311626

Stefano
  • 4,730
  • 1
  • 20
  • 28
0

With Android 10 you won't by default be able to access the file directly by path as Google are forcing people to use Storage Access Framework and or MediaStore to access files.

With Android 11 their plan is to make this the only way to access files.

See https://developer.android.com/training/data-storage/files/external-scoped for details.

You will need to use ContentResolver#openFileDescriptor(Uri, String) and ParcelFileDescriptor#getFileDescriptor() to get something usable

e.g.

ParcelFileDescriptor pfd =
                    this.getContentResolver().
                            openFileDescriptor(Uri, "r");

            FileInputStream fileInputStream =
                    new FileInputStream(
                            pfd.getFileDescriptor());


If you want to get other info about the file you can do that with a contentResolver Query on the URI for DISPLAY_NAME or MIME_TYPE for type of file.

e.g.

// useful name to display to user
  Cursor cursor = this.getContentResolver()
    .query(Uri, new String[] { MediaStore.Files.FileColumns.DISPLAY_NAME},
    null, null, null);

    cursor.moveToFirst();
    filename = cursor.getString(0);
    cursor.close();
Andrew
  • 8,198
  • 2
  • 15
  • 35