是否可以从代码中调用默认图库,以便向用户显示其图库的所有图片以选择一张图片。因此,我需要接收所选图片的路径,以便我可以对其进行处理。
谢谢
使用以下代码创建并触发启动图库的意图
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 0);
和下面的onActivityResult(int requestCode, int resultCode, Intent data)
方法来获取 URI
Uri dataUri = data.getData(); //Image URI
您很可能需要以下方法将此 Image URI 转换为小尺寸位图。如果在您的显示器中显示图像,如果没有它,可能会导致 OOM 异常
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 140;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
}