Android传递Bitmap对象在两个Activity之间

2019-12-10 19:18:20丽君

对应另外一个Activity中实现读取与组装Bitmap对象显示的代码如下:

package com.example.sharedemo; 
 
import java.io.FileNotFoundException; 
 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.widget.ImageView; 
import android.widget.TextView; 
 
public class ImageProcessActivity extends Activity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.share_info); 
    backFillData(); 
  } 
 
  private void backFillData() { 
    Object obj = this.getIntent().getExtras().get("tiger"); 
    ImageView imageView = (ImageView)this.findViewById(R.id.imageView1); 
    TextView text1 = (TextView)this.findViewById(R.id.textView1); 
    TextView text2 = (TextView)this.findViewById(R.id.textView2); 
    try { 
      if(obj != null && obj instanceof ImageInfoBean) 
      { 
        ImageInfoBean dto = (ImageInfoBean)obj; 
        Bitmap bitmap = BitmapFactory.decodeStream(this.openFileInput(dto.getUri())); 
        imageView.setImageBitmap(bitmap); 
        imageView.invalidate(); // refresh 
         
        text1.setText("名称: " + dto.getName()); 
        text2.setText("描述: " + dto.getDescription()); 
        return; 
      } 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } 
     
    Bitmap bitmap = (Bitmap) this.getIntent().getParcelableExtra("selectedImage"); 
    String name = this.getIntent().getExtras().getString("name"); 
    String description = this.getIntent().getExtras().getString("description"); 
    if(bitmap != null) 
    { 
      imageView.setImageBitmap(bitmap); 
      imageView.invalidate(); // refresh 
    } 
    if(name != null) 
    { 
      text1.setText("名称: " + name); 
    } 
    if(description != null) 
    { 
      text2.setText("描述: " + description); 
    } 
  } 
 
} 

对应的Java串行化对象类代码如下:

package com.example.sharedemo; 
 
import java.io.Serializable; 
 
public class ImageInfoBean implements Serializable { 
  /** 
   * 
   */ 
  private static final long serialVersionUID = 1L; 
 
  public String getName() { 
    return name; 
  } 
 
  public void setName(String name) { 
    this.name = name; 
  } 
 
  public String getDescription() { 
    return description; 
  } 
 
  public void setDescription(String description) { 
    this.description = description; 
  } 
 
  public String getUri() { 
    return uri; 
  } 
 
  public void setUri(String uri) { 
    this.uri = uri; 
  } 
 
  private String name; 
  private String description; 
  private String uri; 
 
}