我有一些代码可以从相机中捕获图像并将图像保存在设备库中。在onActivityResults方法之后,我得到图像网址,我可以在设备上的图库中看到图像。但由于某种原因我不能称这个图像。我得到的网址字符串是/storage/emulated/0/Pictures/1493836160696.jpg。任何想法?
public void onClick(View v) {
switch (v.getId()) {
case R.id.cancel_btn:
finish();
break;
case R.id.next_btn:
break;
case R.id.camera_btn:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
takePictureIntent
.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
break;
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String picturePath = cursor.getString(column_index_data);
Uri u = Uri.parse(picturePath);
Log.d("path", String.valueOf(u));
//ALBUM_ART_URI = Uri.parse(String.valueOf(picturePath));
Picasso.with(AddNewDish.this).load(u).into(mImageView);
}
}
}
答案 0 :(得分:0)
将case
中的代码替换为:
case REQUEST_IMAGE_CAPTURE:
if (resultCode == RESULT_OK) {
Picasso.with(AddNewDish.this).load(mCapturedImageURI).into(mImageView);
}
此外,您需要确保在保存的实例状态mCapturedImageURI
中保留Bundle
,因为您的进程可能会在相机应用程序位于前台时终止。
请参阅this sample app了解如何使用ACTION_IMAGE_CAPTURE
以及生成的Uri
。