我正在开发一个Android应用程序,我需要从该应用程序中捕获图像并返回捕获uri
并将其传递给api。我正在oreo 8
上工作。我在stackoverflow上尝试了许多解决方案,但都无法正常工作。
我在onActivityResult
中得到了空uri。
我试图直接从这样的数据中获取它:
uri = data.getData();
我也试图从Bitmap中获取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" />
我错过了什么? 谢谢!
答案 0 :(得分:1)
对于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);
在onActivity结果中
if (requestCode == 101) {
Uri contentUri = FileProvider.getUriForFile(getContext(), "com.example.provider", new File(mCurrentPhotoPath)); //You wll get the proper image uri here.
}
createImageFile()
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;
}