【发布时间】:2019-08-25 08:22:17
【问题描述】:
在我的应用程序中,有一个按钮可以启动相机意图并将捕获的图像保存在给定路径。它在 Nexus 5X 模拟器中运行良好,但是当我在我的 S8 或 S9 中尝试它时,它不会打开相机,也不会显示任何错误。 如何在我的应用中修复相机意图
private void PictureTakerAction()
{
Intent takePic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePic.resolveActivity(getPackageManager()) != null)
{
File photoFile = null;
photoFile = CreatePhotoFile();
if(photoFile != null)
{
pathToFile = photoFile.getAbsolutePath();
Uri photoUri = FileProvider.getUriForFile(AddItemActivity.this,"com.airtechsolutions.fileprovider",photoFile);
takePic.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePic,1);
}
}
}
private File CreatePhotoFile()
{
String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
//File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = null;
try {
image = File.createTempFile(name ,".jpg" , storageDir);
} catch (Exception e) {
Log.i("File Log", "Exception: " + e.toString());
}
return image;
}
protected void onActivityResult(int requestCode,int resultCode, Intent data)
{ super.onActivityResult(requestCode,resultCode,data);
if (requestCode == 1)
{
Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
Image.setImageBitmap(bitmap);
}
}
【问题讨论】:
-
什么是
photoFile?在您定义的地方发布 -
我已经用我用于相机意图的代码更新了问题
-
是否需要将捕获的
Bitmap存储在临时文件中? -
我不明白这个问题,但如果你的意思是我需要一个文件来存储图像然后“是的,我需要它显示在我的应用程序中”
-
检查我的答案。