目前,我正在尝试创建一个共享 Intent,以便在您单击“共享”按钮共享图像时在 Android 应用程序本身中调用。但是,随着 Android Nougat 中 Sharing Intents 功能的改变,我在这一行收到 NullPointerException:
Uri uImageFile = FileProvider.getUriForFile(DetailActivity.this, DetailActivity.this.getApplicationContext().getPackageName(), imageFile);
这是我收到的错误:
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:584)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:558)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
这是我的 Share Intent 方法的完整代码:
private void shareIntent() {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpg");
File imageFile = new File(data.get(pos).getUrl());
Uri uImageFile = FileProvider.getUriForFile(DetailActivity.this, DetailActivity.this.getApplicationContext().getPackageName(), imageFile);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uImageFile);
startActivity(Intent.createChooser(sharingIntent, getResources().getText(R.string.share_to)));
}
data.get(pos).getUrl()
返回我创建的实现了 Parcelable
的自定义类的 URL,当打印出来时,它返回一个目录像下面这样:
“/storage/emulated/0/Pictures/primitive/Primitive-79538313.jpg”
感谢您提供的任何帮助!
最佳答案
将你的代码改成这样
private void shareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jepg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+data.get(pos).getUrl()));
startActivity(Intent.createChooser(shareIntent, "Share image"));
}
将此代码添加到您的 Activity onCreate()
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
关于java - Android Nougat 中具有共享 Intent 的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46921968/