Android - 如何从SD卡中加载图像将图像发送到其他活动

时间:2013-07-24 02:50:19

标签: android-intent android-imageview

我想问一下将路径图像发送到其他活动并显示给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));

从SD卡加载图片后的屏幕截图

![从SD卡加载图像后的屏幕截图] [1]

如果我点击按钮正确,则意图编辑图像

代码有什么问题? 如果你帮助我,你节省了我的时间:(

2 个答案:

答案 0 :(得分:0)

根据您的描述,您想要将图像路径从一个活动传递到另一个活动,您必须首先检查您获得的图像路径,是否正确。然后在另一个活动中,检查路径是否正确传输。

答案 1 :(得分:0)

onActivityResult

中删除这些行
if(requestCode == INTENT_IMAGE){
    Intent intent = new Intent(this, EditImage.class);
    intent.putExtra("intent_image", path);
    startActivity(intent);
}

并使用

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