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
- Android Bitmpafun se bloquea en BitmapFactory.decodeFileDescriptor
- Cómo centrar el texto en android IconGenerator
- Android sin memoria al asignar variables
- Superposición de color en android NDK
- Cambiar el color del mapa de bits
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
- Carga Android de mapas de bits grandes
- Cómo crear un mapa de bits de un archivo de imagen en Android
- Cómo comprobar si un mapa de bits está vacío (en blanco) en Android
- Crear mapa de bits desde el área de pantalla especificada
- Android: el caché de mapas de bits requiere mucha memoria
- Android: El código QR generado que usa Zxing tiene márgenes (no es apropiado para el área)
- Utilizar Picasso para obtener una devolución de llamada con un mapa de bits
- Android PNG a Bitmap - SkImageDecoder :: Factory devuelto null
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.
- Android studio gradle refresh failed (El sistema no puede encontrar el archivo especificado)
- No hay icono para el diálogo de alerta