2012-07-30 69 views
4

我需要改變活動的背景動態,通過從其他活動選擇圖像。並將選定的圖像傳遞迴調用活動,但是當我嘗試獲取圖像時,它是空的。 這是我的代碼。不能傳遞活動之間的位圖在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幫助

回答

10

有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