0

我正在尝试在 ImageView 上绘制位图,但它没有显示...我创建了位图并存储在 Intent Extra 上,但是在新活动中我无法在 ImageView 上绘制它。

这是我的代码,调用新活动:

public void onClickShare(View v) {

        Intent myIntent = new Intent(this, Share.class);

        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);

        myIntent.putExtra("createdImg", b);
        startActivity(myIntent);

    }

关于新活动:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share);

        backgroundImg = (ImageView)findViewById(R.id.imageView1);

        Intent myIntent = getIntent();      
        Bitmap bitmap = (Bitmap) myIntent.getParcelableExtra("createdImg");

        backgroundImg.setImageBitmap(bitmap);
    }

我错过了什么?谢谢!

4

3 回答 3

0

你也可以这样尝试

Bundle bundle = new Bundle();
bundle.putParcelable("createdImg", b);
intent.putExtras(bundle);

并在下一个活动中

Bundle bundle = this.getIntent().getExtras();
Bitmap bmap = bundle.getParcelable("createdImg");

如本教程中给出的http://android-er.blogspot.com/2011/10/pass-bitmap-between-activities.html

于 2013-11-06T00:00:26.333 回答
0

尝试将其作为 ByteArray 发送并在接收活动中对其进行解码。

ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Intent in1 = new Intent(this, Share.class);
in1.putExtra("image",byteArray);

在第二个活动中

byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
于 2013-11-05T22:40:16.837 回答
0

我从每个答案以及其他研究中吸取了一点,我想出了下面的代码,效果很好:

调用新活动:

public void onClickShare(View v) {

        Intent myIntent = new Intent(this, Share.class); 

        // create bitmap screen capture
        Bitmap bitmap;
        View v1 = v.getRootView();
        v1.setDrawingCacheEnabled(true);
        bitmap = Bitmap.createBitmap(v1.getDrawingCache());

        // create a new one with the area I want
        Bitmap bmpLast = Bitmap.createBitmap(bitmap, 25, 185, 430, 520);        
        v1.setDrawingCacheEnabled(false);

        //create an empty bitmap with the size I need
        Bitmap bmOverlay = Bitmap.createBitmap(480, 760, Bitmap.Config.ARGB_8888);

        //draw the content on the bitmap
        Canvas c = new Canvas(bmOverlay); 
        c.drawBitmap(bmpLast, 0, 0, null);

        //compress the bitmap to send to other activity
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmpLast.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();    

        //store compressed bitmap 
        myIntent.putExtra("createdImg", byteArray);     

        startActivity(myIntent);
    }

关于新活动:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.share);

    //decompress the bitmap
    byte[] byteArray = getIntent().getByteArrayExtra("createdImg");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

    //set the imageview with the bitmap
    backgroundImg = (ImageView)findViewById(R.id.imageView1);
    backgroundImg.setImageDrawable(new BitmapDrawable(getResources(), bmp));
}

感谢大家!=)

于 2013-11-06T21:26:35.207 回答