0

hi i am pretty new to android programming, this might be a simple problem for some but i am quite new to this. i want to open the gallery from one intent but want the picked image to be displayed on another intent i used the following code

public class GalActivity extends Activity {
    Context ctx;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView) findViewById(R.id.result);
    }

    private static final int REQUEST_CODE = 1;
    protected Bitmap bitmap;

    private ImageView imageView=null;

    /** Called when the activity is first created. */
    //  @Override

    public void pickImage(View View) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, REQUEST_CODE);
    }

    // ctx=this;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
            try {
                // We need to recyle unused bitmaps
                if (bitmap != null) {
                    bitmap.recycle();
                }
                InputStream stream = getContentResolver().openInputStream(
                        data.getData());
                bitmap = BitmapFactory.decodeStream(stream);
                stream.close();

                imageView.setImageBitmap(bitmap);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

the code works fine if the image is to be displayed on the same intent. but what should i do if it has to be displayed on another intent say "activity2".

4

1 に答える 1

0

BitmapはParcelableInterfaceを実装しているため、インテントを介して直接送信できるため、onActivityResultコードで次のようにします。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
        try {
            // We need to recyle unused bitmaps
            if (bitmap != null) {
                bitmap.recycle();
            }
            InputStream stream = getContentResolver().openInputStream(
                    data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();

          imageView.setImageBitmap(bitmap);

          Intent intent= new Intent(this, SecondActivity.class);
          intent.putExtra("BitmapImage", bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    super.onActivityResult(requestCode, resultCode, data);
}  

次の方法で、このビットマップオブジェクトを別のアクティビティで読みます。

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
于 2012-04-14T20:49:58.930 に答える