当前位置: 代码迷 >> java >> 如何通过结果裁剪活动在MainActivity中设置Bitmap ImageView?
  详细解决方案

如何通过结果裁剪活动在MainActivity中设置Bitmap ImageView?

热度:68   发布时间:2023-07-31 12:03:35.0

您好,我使用此从相机中裁剪照片。 但是没有将结果设置为ImageView,所以

我像这里修改了main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <Button android:id="@+id/button" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="Take picture" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mImageView" />
</LinearLayout>

这是MainActivity.class

    public class MainActivity extends Activity {
    private static final int PICK_FROM_CAMERA = 1;
    private Uri mImageCaptureUri;
    private ImageView mImageView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button b = (Button) findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                doTakePhotoAction();
            }
        });
        mImageView = (ImageView) findViewById(R.id.mImageView);
    }

    private void doTakePhotoAction() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                "tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

        try {
            intent.putExtra("return-data", false);
            startActivityForResult(intent, PICK_FROM_CAMERA);
        }
        catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }


        switch (requestCode) {
            case PICK_FROM_CAMERA:
                Intent intent = new Intent(this, CropImage.class);
                intent.putExtra("image-path", mImageCaptureUri.getPath());
                intent.putExtra("scale", true);
                startActivity(intent);
                break;
        }
    }
}

那么如何从结果裁剪活动中在MainActivity中设置Bitmap ImageView?

尝试这个:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) {
        return;
    }
    if (requestCode == PICK_FROM_CAMERA ) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.getParcelable("data");
        mImageView.setImageBitmap(imageBitmap);
    }


}

通过在此处查找代码: :

按照以下步骤做一些事情:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != RESULT_OK) {
        return;
    }

    switch (requestCode) {
        case PICK_FROM_CAMERA:
            if (data != null && "inline-data".equals(data.getAction())) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.getParcelable("data");
                mImageView.setImageBitmap(imageBitmap);
            }
            else {
                Intent intent = new Intent(this, CropImage.class);
                intent.putExtra("image-path", mImageCaptureUri.getPath());
                intent.putExtra("scale", true);
                intent.putExtra("return-data", true);
                startActivityForResult(intent, 1);
            }
            break;
    }

}
  相关解决方案