我使用以下代码从相机捕获图像并将其存储在公共目录图片中。如何使用相同的图像并将其设置为ImageView?
public void take_photo(){
if (ActivityCompat.checkSelfPermission(Contact_info.this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.CAMERA},40);
}
return;
}
Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (ActivityCompat.checkSelfPermission(Contact_info.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},50);
}
return;
}
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String pictureName = "Image1.jpg";
File imageFile = new File(pictureDirectory, pictureName);
Uri pictureUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pictureUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", imageFile);
} else{
pictureUri = Uri.fromFile(imageFile);
}
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
我的活动结果代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
if(requestCode==CAMERA_REQUEST){
//Commented since upon using this and trying to save the image in the file, the app crashes giving a null pointer exception
//Bitmap cameraImage =(Bitmap) data.getExtras().get("data");
//contact_image.setImageBitmap(cameraImage);
}
}
}
我不想知道如何在ImageView中设置图像,我想知道如何使用相机拍摄并存储在手机目录中的图像作为ImageView中的图像
答案 0 :(得分:1)
public void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mCapturedImageURI = Uri.fromFile(getOutputMediaFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intent, 2);
}
private static File getOutputMediaFile(){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "CameraDemo");
if (!mediaStorageDir.exists()){
if (!mediaStorageDir.mkdirs()){
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}
从上面的方法获得的mcapturedUri设置图像,我希望你正在处理权限&结果部分
答案 1 :(得分:0)
在您的活动中尝试此结果
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);
}
}
答案 2 :(得分:0)
以下是完整的工作代码:
private void dispatchTakePictureIntent() {
Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = Utils.createImageFile(getContext());
chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
photoURI = Uri.fromFile(f);
startActivityForResult(chooserIntent, REQUEST_PHOTO_CAMERA);
}
private void checkCameraPermission() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
new AlertDialog.Builder(getContext())
.setMessage(getString(R.string.camera_request_cancelled))
.setPositiveButton(R.string.dismiss, null)
.setOnDismissListener(dialog -> requestPermissions(
new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA))
.show();
} else {
requestPermissions(new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
}
} else {
dispatchTakePictureIntent();
}
} else {
dispatchTakePictureIntent();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
dispatchTakePictureIntent();
}
}
break;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
/*if (requestCode == REQUEST_PHOTO_CAMERA && resultCode == getActivity().RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
receiptImage.setImageBitmap(imageBitmap);
savePhoto(imageBitmap);
}
*/
if (requestCode == REQUEST_PHOTO_CAMERA && resultCode == Activity.RESULT_OK) {
if (photoURI != null) {
Uri selectedImage = photoURI;
getActivity().getContentResolver().notifyChange(selectedImage, null);
Bitmap reducedSizeBitmap = Utils.getBitmap(photoURI.getPath());
if (reducedSizeBitmap != null) {
receiptImage.setImageBitmap(reducedSizeBitmap);
savePhoto(reducedSizeBitmap);
} else {
Toast.makeText(getContext(), "Error while capturing Image", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getContext(), "Error while capturing Image", Toast.LENGTH_SHORT).show();
}
}
}
private void savePhoto(Bitmap bitmap) {
FileOutputStream outputStream = null;
File file = null;
try {
file = File.createTempFile("photo", null, getContext().getCacheDir());
outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
photoURI = Uri.fromFile(file);
} catch (IOException e) {
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
}
}
uploadImage(file);
}