我无法从 URI 或文件路径更新我的 ImageViews - 它们只是不显示图像
Intent 捕获图像:
Intent photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photo, 1);
在 ActivityResult 上
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Uri imageUri = data.getData();
filePath = getRealPathFromURI(this, imageUri);
}
GetRealPathFromURI 类:
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
然后它将 onActivityResult 中的 'filePath' 插入到数据库中
从数据库中检索并更新 ImageViews
imgfilepath[y] = cursorc.getString(cursorc.getColumnIndex("IMAGE"));
imgFile[y] = new File(imgfilepath[y]);
Uri uri = Uri.fromFile(imgFile[y]);
String path = uri.getPath();
mImage.setImageURI(uri);
我已经尝试了很多不同的方法来设置 ImageBitmap 等,但都没有用(我不记得所有方法了)- 谁能看出为什么不显示图像?
图像在模拟存储中而不是 SD 卡中。
编辑:
我已经添加了 EXTRA_OUTPUT 标签,但是我在 DDMS 中的任何地方都看不到图像,并且在拍照/接受图像后相机没有退出
Intent photo = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
root.mkdirs();
final String fname = "img_"+ System.currentTimeMillis() + ".jpg";
final File sdImageMainDirectory = new File(root, fname);
mImageUri = Uri.fromFile(sdImageMainDirectory);
photo.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(photo, 1);
最佳答案
Uri imageUri = data.getData();
ACTION_IMAGE_CAPTURE
不返回 Uri
。
The image is in emulated storage
也许一个相机应用程序确实如此,在这种情况下,相机应用程序有一个错误,伴随着 return-a-Uri
错误。
Android 设备型号有数千种。这些相机预装了数百种不同的相机应用程序,Play 商店和其他地方还有数百种应用程序可供使用。许多将有 ACTION_IMAGE_CAPTURE
实现。大多数应该遵循 the documented protocol .没有人应该根据您的请求保存图像,因为您没有告诉相机应用程序保存图像的位置。
或者:
通过
EXTRA_OUTPUT
为相机应用程序提供一个位置来保存图像,然后从该位置加载图像,或者如果您不提供
EXTRA_OUTPUT,则使用
data.getExtra("data")
获取代表缩略图大小的图像的Bitmap
关于android - ImageView 不从文件路径填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36774189/