不能传递活动之间的位图在Android中

问题描述:

我需要改变活动的背景动态,通过从其他活动选择图像。并将选定的图像传递回调用活动,但是当我尝试获取图像时,它是空的。 这是我的代码。不能传递活动之间的位图在Android中

public class SelectGoalBackground extends OrmLiteBaseActivity<DatabaseHelper> {// implements{ 



GridView gridview ; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.select_goal_background); 
    final String from = getIntent().getExtras().getString("from"); 
    goalsList = new ArrayList<Goal>(); 

    try { 
     goalsList = getTop3Goals(); 
    } catch (SQLException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (java.sql.SQLException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 


    gridview = (GridView) findViewById(R.id.gridview); 
    gridview.setAdapter(new ImageAdapter(this)); 

    gridview.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 

      ImageView l = (ImageView)gridview.getChildAt(position); 
       Drawable path= l.getDrawable(); 


      Bitmap bitmap = ((BitmapDrawable)path).getBitmap(); 

      Intent intent = new Intent(SelectGoalBackground.this, AlarmAppActivity.class); 

      intent.putExtra("Bitmap", bitmap); 
      startActivity(intent); 
      SelectGoalBackground.this.finish(); 
      return; 

    }); 

} 


public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { // if it's not recycled, initialize some attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 
     Bitmap bm = BitmapFactory.decodeResource(getResources(), mThumbIds[position]); 
     imageView.setImageBitmap(bm); 
     return imageView; 
    } 

    // references to our images 
    private Integer[] mThumbIds = { 
      R.drawable.sample_0, R.drawable.sample_1 

    }; 
} 

,并呼吁活动,我得到这个位图这样

RelativeLayout rel_lay = (RelativeLayout) findViewById(R.id.main_lay); 
    Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap"); 


    if (bitmap != null) { 

     Log.v("bitmap", "not null"); 
     Drawable drawable = new BitmapDrawable(getResources(), bitmap); 
     rel_lay.setBackgroundDrawable(drawable); 
    } 

但位为空在这里,它不设置此活动的背景。 任何形式的帮助表示赞赏,plz帮助

有3个解决方案来解决这个问题。

1)第一转换图像转换成字节数组,然后通入意向和下一个活动从捆绑得到字节数组,转换为图像(位图),并设置成ImageView的。

转换位图字节数组: -

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

通行证字节数组到意图: - 从捆绑

Intent intent = new Intent(this, NextActivity.class); 
intent.putExtra("picture", byteArray); 
startActivity(intent); 

获取字节数组,转换成位图图像: -

Bundle extras = getIntent().getExtras(); 
byte[] byteArray = extras.getByteArray("picture"); 

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
ImageView image = (ImageView) findViewById(R.id.imageView1); 

image.setImageBitmap(bmp); 

2)首先将图像保存到SDCard中,然后在下一个活动中将此图像设置为ImageView。

3)合格的位图到意向并得到了来自包下一个活动的位图,但问题是,如果你的位图/图像大小是当时的图像不会在接下来的活动负载大。

+0

迪帕克感谢ü这么多,我才知道的原因,我的形象是非常大的规模和这就是为什么经过bytesarray和位图都没有工作。我认为将图像保存到SD卡解决方案将起作用,或者我应该调整图像大小。任何建议?谢谢 – 2012-07-30 17:47:08

+0

@MobileDevFast是的,你可以使用bitmap.createscaledbitmap重新调整图像大小 – 2012-07-31 04:20:32

+0

好吧,谢谢一堆:) – 2012-07-31 21:06:34