我正在创建一个聊天应用程序,在其中我从WhatsApp之类的画廊中上传图像和视频,但是当我尝试选择已由WhatsApp文件夹下载的图像时,我会得到这个路径
/storage/emulated/0/WhatsApp/Media/WhatsAppImages/IMG-20180717-WA0024.jpg
但是当我阅读本文以获得显示图像的真实路径时,我得到了这个异常:
java.io.FileNotFoundException:打开失败:ENOENT(无此类文件或目录)
这是我的提供者
<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媒体上发生,请告诉我解决方案。
预先感谢
答案 0 :(得分:1)
android:authorities = “ $ {applicationId}” ,此处应用程序ID包含您的项目包名称。
在您的应用gradle文件中定义的
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文件应为
<?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代码类似。 文件是您要访问的文件路径
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 路径后,可以执行此操作。
要获得真实的路径,请尝试此操作。
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;
}
答案 1 :(得分:0)
当我将视频从whatsapp共享到我的应用程序时,错误抛出列'_data'不存在 java.lang.IllegalArgumentException:列_data不存在