Android:图库

时间:2013-10-09 09:16:11

标签: android

我正在开发用于编辑照片的应用程序。

我在第一个活动中有一个按钮,在第二个活动中有ImageView。当我点击按钮它会打开画廊,我将能够选择一个图像。所选图像需要在第二个活动中出现在我的ImageView中,但事实并非如此。暂时我在第一个活动中显示图像,但是任何人都可以建议我如何在下一个活动中显示该图像。

以下是我的代码。

public class Camera_Gallery_Option extends Activity {

    private static final int CAMERA_REQUEST = 1888;
    private static final int SELECT_PICTURE = 1;
    private String selectedImagePath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_gallery_option);
        Button galleryButton= (Button) findViewById(R.id.button1);

        galleryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"),
                    SELECT_PICTURE);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                imageview.setImageURI(selectedImageUri);
            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

1 个答案:

答案 0 :(得分:1)

selectedImageUri 传递给第二个活动,并在第二个活动中将uri设置为图像视图?

startActivity(new Intent(this, SecondActivity.class).setData(selectedImageUri));

在SecondActivity中:

protected void onCreate(Bundle savedInstanceState) {
    ...
    imageView.setImageURI(getIntent().getData());
}