如何阅读WhatsApp媒体路径 [英] How to read WhatsApp media path

查看:176
本文介绍了如何阅读WhatsApp媒体路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个聊天应用程序,在其中我从WhatsApp之类的画廊中上传图像和视频,但是当我尝试选择已由WhatsApp文件夹下载的图像时,我会得到这个路径

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:打开失败:ENOENT(没有这样的文件或目录)

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

这是我的提供者

<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>

下面是文件路径

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

这仅在WhatsApp媒体上发生,请告诉我解决方案.

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

预先感谢

推荐答案

android:authorities = "$ {applicationId}" ,此处的应用程序ID包含您的项目包名称.

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

在您的应用gradle文件中定义的

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>

并且您的xml文件应为

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>

和您的Java代码类似.这里文件是您要访问的文件路径

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);
                }

在进入 uri 路径后,可以执行此操作.

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;
    }

这篇关于如何阅读WhatsApp媒体路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆