[英]Set ImageView with picture taken by the user
I would like to set the drawable source of my ImageView
with the picture taken by the user but it's not working. 我想用用户拍摄的图片设置ImageView
的可绘制源,但是它不起作用。
This is my onActivityResult
when the user take a picture : 用户拍照时,这是我的onActivityResult
:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
Intent intent = new Intent(this, MyNewActivity.class);
intent.putExtra("URI", imageUri.toString());
startActivity(intent);
}
}
And this is the part of the Activity
where I want to set the resource of my ImageView
: 这是Activity
的一部分,我要设置ImageView
的资源:
if (getIntent().getStringExtra("URI") != null) {
File imgFile = new File(getIntent().getStringExtra("URI"));
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
contactImage.setImageBitmap(myBitmap);
}
}
When I try this, I have a path like this : " file///storage/emulated/O/fname_xxxx.jpg " but it's not working. 当我尝试此操作时,我的路径是这样的:“ file /// storage / emulated / O / fname_xxxx.jpg ”,但是它不起作用。
I try with a picture picker and it work (with a path like "/storage/emulated/..." )! 我尝试使用图片选择器,它可以正常工作(使用“ / storage / emulated / ...”之类的路径)! I really don't know why... 我真的不知道为什么
File imgFile = new File(getIntent().getStringExtra("URI"));
Translates to 转换为
File imgFile = new File("file:///storage/emulated/O/fname_xxxx.jpg");
But it should translate to 但这应该转化为
File imgFile = new File("/storage/emulated/O/fname_xxxx.jpg");
So remove "file://"
before use. 因此,请在使用前删除"file://"
。
This worked for me 这对我有用
if (getIntent().getStringExtra("URI") != null) {
Uri myUri = Uri.parse(extras.getString("URI"));
Picasso.with(RegisterActivity.this)
.load(selectedImage)
.into(contactImage);
}
Try Below Code for both Pic taken by Camera & also pick by gallery 对于相机拍摄的照片以及按图库选择的照片,请尝试以下代码
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
profileIV.setImageBitmap(thumbnail);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), thumbnail);
// CALL THIS METHOD TO GET THE ACTUAL PATH
file = new File(getRealPathFromURI(tempUri));
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), bm);
// CALL THIS METHOD TO GET THE ACTUAL PATH
file = new File(getRealPathFromURI(tempUri));
} catch (IOException e) {
e.printStackTrace();
}
}
profileIV.setImageBitmap(bm);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
To click Image 单击图像
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("URI",Intent.URI_INTENT_SCHEME);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
Replace onActivityResult() method with below code 用下面的代码替换onActivityResult()方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("URI");
imageView.setImageBitmap(photo);
}
}
I think you are parcel wrong URI data in intent Bundle try below code 我认为您在Intent Bundle中打包了错误的URI数据,请尝试以下代码
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
Intent intent = new Intent(this, MyNewActivity.class);
intent.putExtra("URI", data.getData().toString());
startActivity(intent);
}
}
this may help 这可能会有所帮助
I found the solution replacing the "file://" by "" : 我找到了用“”替换“ file://”的解决方案:
File imgFile = new File(getIntent().getStringExtra("URI").replace("file://", ""));
So I get : 所以我得到:
if (getIntent().getStringExtra("URI") != null) {
File imgFile = new File(getIntent().getStringExtra("URI").replace("file://", ""));
if(imgFile.exists()){
Picasso.with(this).load(imgFile).into(myImageView);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.