Android 获取相机捕获后存储在gallery中的图像的路径
我用下面的代码打开相机Android 获取相机捕获后存储在gallery中的图像的路径,android,android-image,Android,Android Image,我用下面的代码打开相机 Intent captureImageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); cordova.setActivityResultCallback(this); cordova.getActivity().startActivityForResult(captureImageIntent,RESULT_CAPTURE_IMAGE); 在ActivityResult的内
Intent captureImageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cordova.setActivityResultCallback(this);
cordova.getActivity().startActivityForResult(captureImageIntent,RESULT_CAPTURE_IMAGE);
在ActivityResult的内部,我试图获取存储在gallery中的图像的路径,以便将其返回到网页
这是我迄今为止所尝试的
Uri uri = intent.getData(); // doesnt work
我试图使用MediaStore.EXTRA_OUTPUT
,但在这种情况下,我得到的是空意图
captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
有人能告诉我怎么才能找到那条路吗
编辑
定义设置和获取捕获图像路径的自定义方法:
private String imgPath;
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");
Uri imgUri = Uri.fromFile(file);
imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == RESULT_CAPTURE_IMAGE) {
imgUserImage.setImageBitmap(decodeFile(getImagePath()));
}
}
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
使用捕获意图将图像uri设置为额外输出:
captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
从解码的捕获图像路径获取捕获图像比特放大器:
private String imgPath;
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");
Uri imgUri = Uri.fromFile(file);
imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == RESULT_CAPTURE_IMAGE) {
imgUserImage.setImageBitmap(decodeFile(getImagePath()));
}
}
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
你能发布如何获取mPhotoUri吗?@hareshchelana查看编辑