[英]How to display images in imageview picked or captured from camera
How to display images in imageview picked or captured from camera I am trying this code but it only work if we pick it from gallery(from camera is not displaying) and image is in rotated form 如何在从相机中拾取或捕获的imageview中显示图像我正在尝试此代码,但是只有当我们从图库中拾取(相机未显示)并且图像处于旋转形式时,此代码才有效
private void pickImage() {
Intent pickIntent = new Intent();
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
pickIntent.setType("image/*");
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String manufacturer = Build.MANUFACTURER;
if(!(manufacturer.contains("samsung")) && !(manufacturer.contains("sony")) && !(manufacturer.contains("lge"))) {
String filename = System.currentTimeMillis() + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, filename);
Uri cameraPicUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPicUri);
}
String pickTitle = "Select or take a new Picture";
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[]{takePhotoIntent}
);
startActivityForResult(chooserIntent, GALLEY_REQUEST_CODE);
}
And onActivityResult() 和onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLEY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
try {
InputStream inputStream = getContentResolver().openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
int height = (int) getResources().getDimension(R.dimen.location_image_width);
int width = (int) getResources().getDimension(R.dimen.location_image_height);
Bitmap smallImage = Bitmap.createScaledBitmap(bitmap, width, height, false);
locationPhoto.setImageBitmap(smallImage);
} catch (Exception e) {
}
if (data == null) {
//Display an error
Constant.showLog("data null ");
return;
}
}
}
In your code you are creating separate intent for camera and gallery. 在您的代码中,您将为相机和图库创建单独的意图。 But your calling only the intent for take photo from gallery. 但是您的呼唤只不过是打算从画廊照相。
Code for take picture and display in image view on a button tap using camera. 使用相机在按钮上轻按即可拍照并在图像视图中显示的代码。 You can refer it. 您可以参考。
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
This works 这有效
It took me some hours to get this working. 我花了几个小时才能完成这项工作。 The code it's almost a copy-paste from developer.android.com, with a minor difference. 该代码几乎是来自developer.android.com的复制粘贴,仅有一点点差异。
Request this permission on the AndroidManifest.xml:
在AndroidManifest.xml:
上请求此权限AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
On your Activity, start by defining this: 在活动上,首先定义以下内容:
static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView;
Then fire this Intent in an onClick:
然后在onClick:
触发此Intent onClick:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.i(TAG, "IOException");
}
// Continue only if the File was successfully created
if (photoFile != null) {
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
Add the following support method: 添加以下支持方法:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
storageDir // directory
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
Then receive the result:
然后接收result:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
mImageView.setImageBitmap(mImageBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
What made it work is the MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath)),
which is different from the code from developer.android.com.
使它起作用的是MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath)),
它与developer.android.com.
的代码不同developer.android.com.
The original code gave me a FileNotFoundException.
原始代码给了我FileNotFoundException.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.