【发布时间】:2023-04-02 07:22:01
【问题描述】:
我正在实现一段从用户单元格的库中获取图片的代码,但我希望用户能够使用 Android 默认裁剪 UI 裁剪图像,所以我使用下面的代码:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 280);
intent.putExtra("outputY", 280);
intent.putExtra("scale", true);
startActivityForResult(intent , RESULT_CODE_PICK_FROM_LIBRARY);
为了取回图像,我使用 onActivityResult 上的代码:
Uri selectedImage = data.getData();
String tempPath = getPath(selectedImage);
Bitmap pickedImage = BitmapFactory.decodeFile(tempPath);
getPath():
private String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA }; // MediaColumns.DATA // MediaStore.Images.Media.DATA
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
return filePath;
} else {
return null;
}
}
但是我得到了一个空指针异常:
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
有人知道这件事吗? 另外,拍照的代码是什么,提供裁剪它的选项,然后检索简单的图像..不需要原始文件..
谢谢, 牛顿
【问题讨论】: