在拍摄图像捕获后在活动结果中获取空 uri



我正在开发一个安卓应用程序,我需要从应用程序获取图像捕获并返回捕获uri以将其传递给 api。我正在研究oreo 8.我在堆栈溢出上尝试了许多解决方案,但没有一个正常工作。 我在onActivityResult得到空 uri . 我试图直接从这样的数据中获取它:

uri = data.getData();

我也试图像这样从位图中获取 URI:

Bitmap photo = (Bitmap) data.getExtras().get("data"); uri = getImageUri(getApplicationContext(), photo);

但仍然不起作用。

这是我当前的代码:

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(hasStoragePermission(IMAGE_CAPTURE)){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, IMAGE_CAPTURE);
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, final int resultCode, Intent data) {
switch(requestCode) {
case 0: {
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
Toast.makeText(getApplicationContext(), "error selecting file!, Please try again ", Toast.LENGTH_LONG).show();
return;
} else {
uri = data.getData();
}
}
break;
}
case 1: {
if (requestCode == IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
if (data == null) {
Toast.makeText(getApplicationContext(), "error selecting file!, Please try again ", Toast.LENGTH_LONG).show();
return;
} else {
uri = data.getData();
/* // Also I try this:
Bitmap photo = (Bitmap) data.getExtras().get("data");
uri = getImageUri(getApplicationContext(), photo); */
}
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}



public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
private boolean hasStoragePermission(int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);
requestPermissions(new String[]{Manifest.permission.CAMERA},
requestCode);
return false;
} else {
return true;
}
} else {
return true;
}
}

这是运行时权限检查器:

private boolean hasStoragePermission(int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);
requestPermissions(new String[]{Manifest.permission.CAMERA},
requestCode);
return false;
} else {
return true;
}
} else {
return true;
}
}

这是我的权限:

<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" />

我错过了什么? 谢谢!

对于android 8.0或更高版本,您必须添加创建文件提供程序才能访问照片。 添加文件提供程序

在清单文件中的"应用程序标记"下添加以下标记。

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
tools:replace="android:resource" />
</provider>

在 xml 文件夹中创建provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>

意图捕获图像

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
if ((Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)) {
photoURI = FileProvider.getUriForFile(getContext(),
"com.example.provider",
photoFile);
//FAApplication.setPhotoUri(photoURI);
} else {
photoURI = Uri.fromFile(photoFile);
}
takePicture.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePicture, 101);

在活动结果中

if (requestCode == 101) {
Uri contentUri = FileProvider.getUriForFile(getContext(), "com.example.provider", new File(mCurrentPhotoPath)); //You wll get the proper image uri here.
}

创建图像文件()

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

/*File file = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
+ File.separator
+ imageFileName);
if (file.getParentFile().exists() || file.getParentFile().mkdirs()) {
mCurrentPhotoPath = file.getAbsolutePath();
}*/

File file = File.createTempFile(
imageFileName,   //prefix
".jpg",          //suffix
storageDir       //directory
);
// Save a file: path for use with ACTION_VIEW intents
return file;
}

最新更新