gpt4 book ai didi

java - android 6.0 文件提供程序在 FileProvider.getUriForFile 上不工作空指针

转载 作者:行者123 更新时间:2023-12-01 16:48:51 31 4
gpt4 key购买 nike

下面的代码用于尝试文件提供程序,为 Android Nougat 做准备:

list 文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.securefilesharing"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="25" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />


<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".HomeSecureFileShareTestActivity"
android:label="@string/title_activity_home_secure_file_share_test" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.securefilesharing.fileprovider"
android:enabled="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_path" />
</provider>

</application>

</manifest>

文件提供者 xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="/"/>
</paths>

构建 URI 并传递给相机 Intent :

private Uri createImageFile() throws Exception {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), System.currentTimeMillis() + ".jpg");
//File file = new File(System.currentTimeMillis() + ".jpg");

Uri photoUri = null;

try{
photoUri = FileProvider.getUriForFile(this, "com.securefilesharing.fileprovider", file);
}
catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}

return photoUri;
}

View.OnClickListener ocl = new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
uri = createImageFile();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} catch (Exception e) {
Log.e(TAG, TAG + ": " + e.toString());
}
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
};

我还做了activity.requestPermissions(forPermissions.toArray(new String[0]), PERMISSION_CODE);对于相机,读写外部文件

下面是异常消息:

06-22 15:04:13.962:E/HomeSecureFileShareTestActivity(31851):HomeSecureFileShareTestActivity:java.lang.NullPointerException:尝试调用虚拟方法'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(空对象引用上的 android.content.pm.PackageManager, java.lang.String)'

最佳答案

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.securefilesharing.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_path" />
</provider>

更改 list 的提供商部分...引用::https://developer.android.com/guide/topics/manifest/provider-element.html#enabled

关于java - android 6.0 文件提供程序在 FileProvider.getUriForFile 上不工作空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44693535/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com