2012-08-17 10 views
27

Estoy usando mi servidor local para buscar imágenes y verlas en un ImageView. Por algún motivo, estoy obteniendo un error nulo de Factory. He revisado el código muchas veces y no veo lo que está mal. ¡Cualquier ayuda sería apreciada!Android: SkImageDecoder :: La fábrica devolvió nulo

GalleryZoom.java

public class Zoom extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.gallery_zoom); 

     String selection = getIntent().getExtras().getString("image"); 
     Toast.makeText(this, selection, Toast.LENGTH_LONG).show(); 

     new backgroundLoader().execute();  
    } 


    private class backgroundLoader extends AsyncTask<Void, Void, Void> { 
     Bitmap bmp; 

     @Override 
     protected Void doInBackground(Void... params) { 

      bmp = DecodeBitmapSampleSize(getIntent().getExtras().getString("image"), 48, 64); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 

      ImageView image = (ImageView) findViewById(R.id.imageZoom); 
      image.setImageBitmap(bmp); 
     } 

    } 

    public Bitmap DecodeBitmapSampleSize (String strURL, int reqWidth, int reqHeight) { 
     InputStream in = null; 
     Bitmap bmp = null; 

     in = OpenHttpConnection(strURL); 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(in, null, options); 

     options.inSampleSize = calculateSampleSize(options, reqWidth, reqHeight); 

     options.inJustDecodeBounds = false; 
     bmp = BitmapFactory.decodeStream(in, null, options); 
       return bmp; 
    } 

    private InputStream OpenHttpConnection(String strURL) { 

     try { 
      URL url = new URL(strURL); 

      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      InputStream in = new BufferedInputStream(connection.getInputStream()); 
      return in; 
     } catch (Exception exception) { 
      exception.printStackTrace(); 
      return null; 
     } 
    } 

    public static int calculateSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 

     final int width = options.outWidth; 
     final int height = options.outHeight; 
     int inSampleSize = 1; 

     if (width > reqWidth || height > reqHeight) { 
      if (width > height) { 
       inSampleSize = Math.round((float) height/(float) reqHeight); 
      } else { 
       inSampleSize = Math.round((float) width/(float) reqWidth); 
      } 
     } 
     return inSampleSize; 
    } 

} 

LogCat Entrar

08-13 21:55:19.578: I/MemoryCache(3197): MemoryCache maximum limit is 6MB 
08-13 21:55:19.658: I/MemoryCache(3197): cache size = 24600, length = 1 
08-13 21:55:19.688: I/MemoryCache(3197): cache size = 24600, length = 1 
08-13 21:55:19.708: I/MemoryCache(3197): cache size = 24600, length = 1 
08-13 21:55:19.708: I/MemoryCache(3197): cache size = 24600, length = 1 
08-13 21:55:20.628: I/MemoryCache(3197): cache size = 71600, length = 2 
08-13 21:55:20.678: I/MemoryCache(3197): cache size = 101408, length = 3 
08-13 21:55:26.228: I/MemoryCache(3197): MemoryCache maximum limit is 6MB 
08-13 21:55:26.228: I/MemoryCache(3197): MemoryCache maximum limit is 6MB 
08-13 21:55:26.998: D/skia(3197): --- SkImageDecoder::Factory returned null 
+0

refieren este post que trabajó para mí: [http://stackoverflow.com/questions/23559736/android-skimagedecoderfactory-returned-null-error][1] [1]: http://stackoverflow.com/questions/23559736/android-skimagedecoderfactory-returned-null-error – mithil1501

Respuesta

59

me he encontrado con el mismo problema. Y también me aseguro de que la URL sea correcta para descargar la imagen. Al depurar el código, encontré que la variable de posición en inputstream se estableció en 1024 después de la primera decodificación. Entonces agrego inputstream.reset() antes de la segunda decodificación. Eso funciona. Hope puede ayudar a otros.

+6

Esto funcionó para mí, pero asegúrese de llamar a '' 'boolean supported = inputStream.markSupported()' '' primero. La marca debe ser compatible o restablecer() arrojará una '' 'IOException'''. Si no es compatible, tal vez abra una segunda secuencia de entrada. – Shellum

+0

¿cómo resolviste esto? Tengo el mismo problema y lo he publicado aquí: http://stackoverflow.com/questions/17774442/how-to-get-bitmap-information-and-then-decode-bitmap-from-internet-inputstream. por alguna razón, no funciona en algunos sitios web y archivos muy específicos. –

1

Tuve un problema similar al leer un flujo de un Intento devuelto desde la aplicación de la galería. inputstream.reset() arroja una IOException, como Shellum menciona arriba, así que lo resolví cerrando la transmisión y volviéndola a abrir. Simple, y lo hizo el truco.

+0

Seguí este consejo estableciendo la posición de la secuencia en 0 (en C#) – Gandalf458

11

Hola chicos, después de mirar alrededor obtuve la mejor solución. como #jia George señala que restableció el flujo de entrada después de la primera decodificación, el problema es que no es posible reiniciarlo alguna vez, pero puede ajustar el flujo de entrada dentro de un BufferedInputStream y este está bien.

final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BufferedInputStream buffer=new BufferedInputStream(is); 
BitmapFactory.decodeStream(buffer,null,options); 
buffer.reset(); 

    // Calculate inSampleSize 
options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight); 

    // Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 
BitmapFactory.decodeStream(buffer,null,options); 
+0

¡lo único que funciona para mí! – Ricardo

11

Esto podría ser un caso raro, pero para aquellos de ustedes usando Picasso verá este error si se intenta cargar una imagen de la URL, pero la URL no se refiere a una imagen.

Por ejemplo:
http://www.google.ca
en lugar de:
https://www.google.ca/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png

+0

Además, algunas URL pueden terminar con '.jpg',' .png', '.tif', etc., pero en realidad no se refieren a una imagen. Por ejemplo: http://www.guoguiyan.com/data/out/96/69698096-high-resolution-wallpapers.jpg. – shoe

Cuestiones relacionadas