Poner una imagen grande en un ImageView no funciona
Creo que hay un problema con mi ImageView. He creado una galería, donde puedo tocar una imagen y ponerla en mi ImageView a continuación:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/fonddegrade"> <Gallery android:layout_height="wrap_content" android:id="@+id/gallery" android:layout_width="fill_parent" /> <ImageView android:layout_below="@+id/gallery" android:layout_height="wrap_content" android:id="@+id/laphoto" android:layout_width="wrap_content" android:layout_centerHorizontal="true"/>
Esto está trabajando perfectamente con la imagen pequeña, pero no con la imagen grande (3264 * 1952). Cuando lo toco (por lo que, tratando de ponerlo en el ImageView), tengo un error y mi accidente de aplicación. Aquí está mi código java para mostrar la imagen:
- Obtener realPath volver null en android marshmallow?
- Uri a la notificación de sonido por defecto?
- Android: Obtener un archivo URI de un URI de contenido?
- Empieza whatsapp con android.content.Intent.ACTION_SENDTO
- ¿Hay lejos para conseguir uri del mapa de bits con hacia fuera excepto él a sdcard?
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.photo); File images; // Trouver le bon endroit de stockage if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) images = new File("/sdcard/MyPerformance/Photo"); else images = this.getFilesDir(); images.mkdirs(); File[] imagelist = images.listFiles(new FilenameFilter(){ @Override public boolean accept(File dir, String name) { return ((name.endsWith(".jpg"))||(name.endsWith(".png"))); } }); mFiles = new String[imagelist.length]; for(int i = 0 ; i< imagelist.length; i++) { mFiles[i] = imagelist[i].getAbsolutePath(); } mUrls = new Uri[mFiles.length]; for(int i = 0; i < mFiles.length; i++) { mUrls[i] = Uri.parse(mFiles[i]); } imgView = (ImageView)findViewById(R.id.laphoto); if(mFiles.length != 0) imgView.setImageURI(mUrls[0]); gallery = (Gallery) findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { imgView.setImageURI(mUrls[position]); } }); }
El problema proviene del setImageURI (pero no creo que sea la causa, ya que es trabajo con imagen pequeña) o por el tamaño de la imagen.
¿Qué solución me puede dar para resolver este problema? Tienes mi agradecimiento.
PS: ¿Por qué mi "Hello" siempre se borra?
- Cómo obtener un uri de un recurso de imagen en android
- Pantalla de chat de Skype desde la intención
- Android data.getData () devuelve null de CameraActivity para algunos teléfonos
- ¿Cómo puedo transformar un Bitmap en un Uri?
- Cómo agregar una cláusula de límite utilizando el proveedor de contenido
- Problema con URI matcher en Android
- Conversión de Uri en String
- Agregar calendario y eventos en Android 2.2
Su imagen es probablemente demasiado grande para Android y se queda sin memoria. Las aplicaciones pueden tener hasta 16Mb de memoria utilizable. Su imagen toma 3264 * 1952 * 4 = ~ 25.5Mb (anchura de altura argb). Así que podría ser mejor para cambiar el tamaño de las imágenes en un tamaño más pequeño.
Ver: http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
Entonces: Problema extraño de memoria al cargar una imagen en un objeto Bitmap
Por último: VM se queda sin memoria mientras obtiene imágenes de la caché
Aquí hay un bitmap [util class para ayudarte con el manejo de los mapas de bits grandes.
import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class BitmapUtils { public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } public static Bitmap decodeSampledBitmapFromResource(String pathToFile, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(pathToFile, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(pathToFile, options); } }
- Aplicación que solicita permiso no declarado – "Probar acceso a almacenamiento protegido"
- ¿Cómo extender ImageButton correctamente?