No se puede pasar mapa de bits entre actividades en android

Necesito cambiar el fondo de la actividad dinámicamente, seleccionando una imagen de otra actividad. Y pasar la imagen seleccionada de nuevo a la actividad que llama, pero cuando intento conseguir la imagen, es null. Aquí está mi código.

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 }; } 

Y en la actividad de llamadas obtengo este bitmap como este

  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); } 

Pero el mapa de bits es nulo aquí y no está configurando el fondo de esta actividad. Cualquier tipo de ayuda es apreciada, ayuda plz

Hay 3 soluciones para resolver este problema.

1) Primero convierta la imagen en matriz de bytes y luego pase a Intent y en la siguiente actividad obtenga la matriz de bytes de Bundle y convierta en imagen (Bitmap) y establezca en ImageView.

Convertir mapa de bits a Byte Array: –

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

Pase la matriz de bytes en la intención: –

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

Obtener Byte Array de Bundle y convertir en mapa de bits Imagen: –

 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) Primero guarde la imagen en SDCard y en la siguiente actividad, establezca esta imagen en ImageView.

3) Pase Bitmap en Intent y obtenga mapa de bits en la siguiente actividad del paquete, pero el problema es si su tamaño de imagen / mapa de bits es grande en ese momento la imagen no se carga en la actividad siguiente.

  • Java.lang.OutOfMemoryError en almacenar imágenes en sqlite db
  • Tamaño de imagen de mapa de bits de Android en xml
  • Utilice MediaCodec y MediaExtractor para decodificar y codificar vídeo
  • Aumentar la memoria asignada a la aplicación
  • Cómo cambiar el color de fondo de mapa de bits a transparente y de fondo no debe arrastrar?
  • Cómo rellenar el objeto de mapa de bits con color en Android
  • Android: cargador de imágenes universal cargar imagen grande
  • Obtenga el tamaño medido y la posición del mapa de bits en el lienzo después de la transformación 2D
  • JNI operaciones de mapa de bits, para ayudar a evitar OOM cuando se utilizan imágenes grandes
  • Cómo manejar try catch exception android
  • Carga de Android desde la URL a Bitmap
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.