2

I am creating a chat application in which I am uploading images and videos from the gallery like WhatsApp, but when I trying select image which is already downloaded by WhatsApp folder then I get this path

/storage/emulated/0/WhatsApp/Media/WhatsAppImages/IMG-20180717-WA0024.jpg

but when I read this to get real path to show image then I am getting this exception:

java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)

This is my provider

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

Below are the file path

<resources>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="/storage/emulated/0" path="."/>
</paths>

This happens only with WhatsApp media anyone please tell me the solution.

Thanks in advance

Ask
  • 41
  • 1
  • 6

2 Answers2

2

android:authorities="${applicationId}" here application id contains your project package name.

which are define in you app gradle file

 defaultConfig {
        applicationId "<your app Id>"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 25
        versionName '5.8'
        multiDexEnabled true
    }

/////

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="<package name>.fileprovider" // which are defined in app gradle application id
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths_public" />
        </provider>

and your xml file should like

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external_files"
        path="." />
</paths>

and your java code are like. here file is your filepath which you are trying to access

Uri uri = null;
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() +
                            ".provider", file);
                } else {
                    uri = Uri.fromFile(file);
                }

after getting path in uri do whatever you want with this.

and for getting real path try this It may work.

public String getRealPathFromURI(Uri contentURI) {
        String result;
        Cursor cursor = context.getContentResolver().query(contentURI, null,
                null, null, null);

        if (cursor == null) { // Source is Dropbox or other similar local file
            // path
            result = contentURI.getPath();
        } else {
            cursor.moveToFirst();
            try {
                int idx = cursor
                        .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                result = cursor.getString(idx);
            } catch (Exception e) {
                AppLog.handleException(ImageHelper.class.getName(), e);
                Toast.makeText(context, context.getResources().getString(
                        R.string.error_get_image), Toast.LENGTH_SHORT).show();

                result = "";
            }
            cursor.close();
        }
        return result;
    }
Learning Always
  • 1,563
  • 4
  • 29
  • 49
  • did you change your android:authorities="${applicationId}" to like android:authorities="com.ex.example.fileprovider". try this – Learning Always Jul 18 '18 at 11:02
  • i get this uri content://com.example.myapp/external_files/WhatsApp/Media/WhatsAppImages/IMG-20180717-WA0024.jpg but still cannot get real path – Ask Jul 18 '18 at 11:06
  • this is your real path above marshmellow device.. you can access this file with that uri.. and you can also convert uri to string if you want . – Learning Always Jul 18 '18 at 11:09
  • If you don't mind send your java code block where you get any error or exception – Learning Always Jul 18 '18 at 11:11
  • Thanks but i want path like this /storage/emulated/0/Download/abc.jpg I want uri to give me this type of path... i am using Oreo version... will you tell me how can i get this type of path or should i store to another folder? – Ask Jul 18 '18 at 11:15
  • Error is resolved as per your provided help link thanks but i want real path as i mentioned above – Ask Jul 18 '18 at 11:17
  • In that case replace this uri mannualy "content://com.example.myapp/external_files" with your /storage/emulated/0 – Learning Always Jul 18 '18 at 11:18
  • according to this .... it can again lead me to my question /storage/emulated/0/WhatsApp/Media/WhatsAppImages/IMG-20180717-WA0024.jpg – Ask Jul 18 '18 at 11:23
  • android.database.CursorIndexOutOfBoundsException: Requested column: -1, # of columns: 2 getting this exception now :( – Ask Jul 18 '18 at 11:51
  • Check ans from this https://stackoverflow.com/questions/19985286/convert-content-uri-to-actual-path-in-android-4-4/27271131#27271131 – Learning Always Jul 18 '18 at 12:03
  • 1
    Permission Denial: opening provider com.whatsapp.MediaProvider – Ask Jul 18 '18 at 12:14
  • Ok. may be you got your solution :) – Learning Always Jul 18 '18 at 12:33
0

Error throw column '_data' does not exist when i share video from whatsapp to my app java.lang.IllegalArgumentException: column '_data' does not exist

Harish
  • 11
  • 4
  • i also use this method.this methos was working in file manager.But i have share video from whatsapp or hike to my app throws exception column_data does not exist and illegalArgumentException Please Help me... – Harish Aug 13 '18 at 12:12