我在开发一款安卓应用,需要从应用中捕获图像并返回 uri
给API使用。我的系统版本为 oreo 8
。我在stackoverflow上尝试了多种解决方法,但都没有正确地起作用。在 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" />
我错过了什么?谢谢!