1

我在将图像从 imageView 发送到另一个活动时遇到问题。我的代码运行良好,但仅用于发送代码中给出的图像而无需更改。我在照片上添加了过滤器,我需要发送带有这些更改的图像。这是我的代码:

第一项活动:

public void send(View view) {
    //trzeba tu coś wymyslić żeby dodawało np tag żeby wiedziec jaka obraz ma nazwe
    //i wstawić do tego niżej
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.i);     
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] b = baos.toByteArray();

    Intent intent = new Intent(this, TwoActivity.class);
    intent.putExtra("picture", b);
    startActivity(intent);
}

下一个活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two);

    Bundle extras = getIntent().getExtras();
    byte[] b = extras.getByteArray("picture");
    Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
    ImageView image = (ImageView) findViewById(R.id.imageView1);
    image.setImageBitmap(bmp);
}

请告诉我我应该改变什么才能正确发送带有更改的图像?

4

1 回答 1

1

相同的原因是因为您只是从资源中传递图像;不是任何被编辑的东西。

由于听起来您想从视图中获取编辑后的图像,因此您可以轻松获取其绘图缓存并使用它。

public void send(View view) {
    Bitmap bitmap = getFromCache(view);     
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] b = baos.toByteArray();

    Intent intent = new Intent(this, TwoActivity.class);
    intent.putExtra("picture", b);
    startActivity(intent);
}

private Bitmap getFromCache(View view){
    view.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); // Make sure to call Bitmap.createBitmap before disabling the cache, as the Bitmap will be recycled once it is disabled again
    view.setDrawingCacheEnabled(false);
    return bitmap;
}
于 2015-03-05T22:14:28.063 回答