拍摄后如何在ImageView中设置照片? Android Studio

时间:2018-06-22 00:13:54

标签: android camera imageview

我正在开发一个要求拍照的应用程序。拍摄照片后,将创建一个文件,并使用文件路径将照片放置在ImageView中。

一切似乎都很好,相机活动开始了,我可以拍照了,但是当我们返回通话活动时,ImageView一直空白。 有什么建议么?

请在下面查看我的代码

 public void takePhotoButtonOnClick(View view) {
    dispatchTakePictureIntent();
    Bitmap bmImg = BitmapFactory.decodeFile(mCurrentPhotoPath);
    photoImage.setImageBitmap(bmImg);

}


static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.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

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "hwardak.employeecenter.FileProvider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}


String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String imageFileName = editText_id.getText().toString().trim() + "_photo_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();

    return image;
}

XML

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/photoImage"
android:layout_gravity="center_horizontal"
android:src="@mipmap/empty_profile"
/>

1 个答案:

答案 0 :(得分:0)

您需要实现方法onActivityResult,然后在那里更新ImageView

另请参阅https://developer.android.com/training/basics/intents/result ...例如:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (resultCode == RESULT_OK && requestCode == REQUEST_TAKE_PHOTO) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

也许下次阅读整个页面,而不仅仅是一半... https://developer.android.com/training/camera/photobasics