问题描述
我的代码在 android 7、8、9 和 11 上运行良好,但是当我在 android 10 上运行应用程序时,我在这一行发现了这个错误 - (java.lang.NullPointerException) (camerauri = FileProvider.getUriForFile(context,context.getApplicationContext().getPackageName() + ".provider",imageFile);)
`camera.setonClickListener(new View.OnClickListener() { @覆盖 public void onClick(View view) {
takenbycamera = true;
if (ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"PPS_Photo_Directory");
if (!path.exists()) {
path.mkdir();
}
File imageFile = null;
try {
imageFile = File.createTempFile("imagesample",".jpg",path);
} catch (IOException e) {
e.printstacktrace();
}
Context context = getBaseContext();
if (Build.VERSION.SDK_INT >= 24)
camerauri = FileProvider.getUriForFile(context,imageFile);
else
camerauri = Uri.fromFile(imageFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT,camerauri);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= 30) {
List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName,camerauri,Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
startActivityForResult(intent,CAMERA_REQUEST);
file = new File(imageFile.getPath());
} else {
requestStoragePermission();
}
}
});
上面是我的相机代码
@覆盖 protected void onActivityResult ( int requestCode,int resultCode,@Nullable Intent data){ super.onActivityResult(requestCode,resultCode,data);
if (resultCode==RESULT_OK){
if (requestCode == CAMERA_REQUEST) {
Log.d("activityresult","camera enter");
if (camerauri !=null)
{
Log.d("activityresult","Uri enter");
Uri selectedImage = camerauri;
if (selectedImage!=null)
{
startCropActivity(selectedImage);
fetchOriginalImageSize(selectedImage);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)