这是我的清单的一部分:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asd"
android:versionCode="118"
android:versionName="118" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:name="com.example.asd.AsdApplication"
android:allowBackup="true"
android:allowTaskReparenting="true"
android:theme="@style/AsdTheme" >
...
<provider
android:name="com.example.asd.database.hq.ContentProviderDB"
android:authorities="ourContentProviderAuthorities" >
</provider>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.asd.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
这是raw/xml/filepaths.xml中的filepaths文件
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="media"/>
</paths>
Run Code Online (Sandbox Code Playgroud)
我从互联网下载视频并以这种方式将其保存到内部存储:
public static boolean saveInputStreamToInternalStorageFile(Context context, String filename, byte[] dataToWrite, Context ctx) {
FileOutputStream fos;
try {
fos = new FileOutputStream(context.getFilesDir() + File.separator + filename);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(dataToWrite);
oos.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我试着像这样使用它:
private void playVideoFromDeviceWithWorkaround(String fileName) {
File newFile = new File(getFilesDir(), fileName);
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
try {
vvVideoFullscreen.setVideoURI(contentUri);
showMediaControls = true;
playVideo();
} catch (Exception e) {
playVideoFromNetwork();
}
}
Run Code Online (Sandbox Code Playgroud)
在这一行:
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)
Run Code Online (Sandbox Code Playgroud)
J. *_* K. 253
问题是在Manifest中我有这条线:
android:authorities="com.example.asd.fileprovider"
Run Code Online (Sandbox Code Playgroud)
当我调用getUriForFile时,我正在传递:
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.asd", newFile);
Run Code Online (Sandbox Code Playgroud)
所以改变"com.example.asd"
了"com.example.asd.fileprovider"
,它起作用了
TWi*_*Rob 42
你可以做到这一点没有用的能够运行在同一台设备(想想在多个变种一个额外的好处硬编码的包名release
和debug
用applicationIdSuffix
,看到这些问题):
基于 FileProvider.java:560
final ProviderInfo info = context.getPackageManager()
.resolveContentProvider(authority, PackageManager.GET_META_DATA);
final XmlResourceParser in = info.loadXmlMetaData( //560
context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
Run Code Online (Sandbox Code Playgroud)
你使用了错误authority
而且没有找到ContentProvider
(info == null
).
${applicationId}
将您的清单更改为(将由Manifest Merger取代)
android:authorities="${applicationId}.share"
Run Code Online (Sandbox Code Playgroud)
和
Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".share", result);
Run Code Online (Sandbox Code Playgroud)
该.share
后缀是可选的,如果你有一个真正的ContentProvider
这是更好地为有权威的包名.
Gia*_*men 22
在我的情况下,我得到了错误,因为
BuildConfig.APPLICATION_ID
Run Code Online (Sandbox Code Playgroud)
正在从中进口
import android.support.v4.BuildConfig;
Run Code Online (Sandbox Code Playgroud)
所以它返回的字符串"android.support.v4"
不是我的项目包名.检查导入文件来自您import project.Buildconfig
而非另一个.例:
import com.example.yourProjectName.BuildConfig;
Run Code Online (Sandbox Code Playgroud)
最后,在<provider>
Manifest 中的标签中,我必须android:authorities="${applicationId}"
始终将项目包名称作为权限
<manifest>
..
..
<application>
..
..
<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/ruta_fileprovider" />
</provider>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
首先,请确保您的提供程序android:authorities
与其他提供程序不冲突。除此之外,您可以为名称的最后一部分选择任何名称:“ provider”,“ fileprovider”等,但是当android:authorities
列出多个列表时,应用程序崩溃,而文档指出它允许列出多个值。
file://
现在,不允许在targetSdkVersion> = 24(Android N 7.0)上使用Intent附加方案,仅对content://
所有设备(Android 5、6和7)传递该方案。但是我们遇到了小米违反此Google约定并发送file://
,因此data.getData().getAuthority()
给出了空字符串。
final String uriScheme = currentUri.getScheme();
if ("content".equals(uriScheme)) {
// getting full file path (works with some providers, i.e. Gallery)
path = FileUtils.getPath(getContext(), currentUri);
if (path != null) {
currentFile = new File(path);
}
} else if ("file".equals(uriScheme)) {
// in a rare case we received file:// in currentUri, we need to:
// 1. create new File variable from currentUri that looks like "file:///storage/emulated/0/download/50044382b.jpg"
// 2. generate a proper content:// Uri for it
currentFile = new File(currentUri.getPath());
String authority = data.getData().getAuthority();
if (authority != null && !authority.isEmpty()) {
currentUri = FileProvider.getUriForFile(getActivity(), authority, currentFile);
}
} else {
// throw exception
}
Run Code Online (Sandbox Code Playgroud)
另外,FileProvider.getUriForFile()
导致崩溃的错误java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example/files/attachments/image.jpg
已在Android支持库v24.2.0中修复。问题是FileProvider.java没有看到外部路径文件夹。
归档时间: |
|
查看次数: |
81511 次 |
最近记录: |