Android - How to Send Image to other activity from

2019-06-14 09:18发布

问题:

i want to ask about send path image to other activity and show to imageview

BrowsePhoto.java

private static int PICK_FROM_FILE = 1;
private static int INTENT_IMAGE = 2;
private ImageButton buttonLoadImage;
private ImageButton reloadPhoto;
private ImageButton imagedone;
private ImageView mImageView;
private Uri mImageCaptureUri;
TextView textImagePath;

Uri imageUri = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.push_left_in, R.anim.hold);
    setContentView(R.layout.browse);

    buttonLoadImage = (ImageButton) findViewById(R.id.btnLoadImage);
    imagedone = (ImageButton) findViewById(R.id.btnPhotoOk);
    reloadPhoto = (ImageButton) findViewById(R.id.btnReLoad);
    mImageView = (ImageView) findViewById(R.id.iv_pic);
    textImagePath = (TextView) findViewById(R.id.imagepath);

    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(
                    Intent.createChooser(intent, "Complete action using"),
                    PICK_FROM_FILE);

            buttonLoadImage.setVisibility(View.INVISIBLE);
            imagedone.setVisibility(View.VISIBLE);
            reloadPhoto.setVisibility(View.VISIBLE);
        }
    });

    findViewById(R.id.btnPhotoOk).setOnClickListener(this);
    findViewById(R.id.btnReLoad).setOnClickListener(this);

}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnPhotoOk:
        moveToEditPhotoDrawerActivity();

        break;
    case R.id.btnReLoad:
        backToBrowsePhotoDrawerActivity();
        break;
    default:
        break;
    }
}

private void moveToEditPhotoDrawerActivity() {
    Intent intent = new Intent();
    startActivityForResult(intent, INTENT_IMAGE);
}

private void backToBrowsePhotoDrawerActivity() {
    Intent intent = new Intent(this, BrowsePhoto.class);
    startActivity(intent);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != RESULT_OK)
        return;

    Bitmap bitmap = null;
    String path = "";

    if (requestCode == PICK_FROM_FILE) {
        mImageCaptureUri = data.getData();

        path = getRealPathFromURI(mImageCaptureUri);

        if (path == null)
            path = mImageCaptureUri.getPath();

        if (path != null)
            bitmap = BitmapFactory.decodeFile(path);
    } else {
        path = mImageCaptureUri.getPath();
        bitmap = BitmapFactory.decodeFile(path);


    }

    mImageView.setImageBitmap(bitmap);

    textImagePath.setText(path.toString());

    if(requestCode == INTENT_IMAGE){
        Intent intent = new Intent(this, EditImage.class);
        intent.putExtra("intent_image", path);
        startActivity(intent);
    }

}

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(contentUri, proj, null, null, null);

    if (cursor == null)
        return null;

    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

    cursor.moveToFirst();

    return cursor.getString(column_index);
}

EditImage.java

    String picturePath = getIntent().getStringExtra("intent_image");
    ImageView imageView = (ImageView) findViewById(R.id.iv_pic);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

Screenshoot after i load image from sd card

![Screenshoot after i load image from sd card][1]

and if i click button correct, then intent to editimage

what is wrong from the code? if you help me, you saved my time :(

回答1:

remove these lines from your onActivityResult

if(requestCode == INTENT_IMAGE){
    Intent intent = new Intent(this, EditImage.class);
    intent.putExtra("intent_image", path);
    startActivity(intent);
}

and change moveToEditPhotoDrawerActivity with

    private void moveToEditPhotoDrawerActivity() {
    Intent intent = new Intent(this, EditImage.class);
    intent.putExtra("intent_image", path);
    startActivity(intent);
}


回答2:

according to your description, you want to pass image path from one activity to another, you have to check the image path you get firstly, whether it is correct. then in another activity, check whether the path is transferred correctly.