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

时间:2015-07-19 09:18:57

标签: java android bitmap

您好我使用此https://github.com/chemalarrea/CropImage从相机裁剪照片。但是没有为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?

2 个答案:

答案 0 :(得分:0)

试试这个:

 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);
    }


}

答案 1 :(得分:0)

从这里查看代码: https://github.com/chemalarrea/CropImage/blob/master/src/com/droid4you/util/cropimage/CropImage.java

做一些事情:

 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;
    }

}