2012-08-17 34 views
27

Tôi đang sử dụng máy chủ cục bộ của mình để tìm nạp hình ảnh và xem trong ImageView. Đối với một số lý do tôi nhận được Nhà máy trả về lỗi null. Tôi đã xem qua mã nhiều lần và tôi không thấy có gì sai. Bất kỳ trợ giúp sẽ được đánh giá cao!Android: SkImageDecoder :: Nhà máy trả về null

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 Log

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

tham khảo bài này nó làm việc cho tôi: [http://stackoverflow.com/questions/23559736/android-skimagedecoderfactory-returned-null-error][1] [1]: http://stackoverflow.com/questions/23559736/android-skimagedecoderfactory-returned-null-error – mithil1501

Trả lời

59

Tôi đã gặp cùng một vấn đề. Và tôi cũng đảm bảo url chính xác để tải xuống hình ảnh. Bằng cách gỡ lỗi mã, tôi thấy biến vị trí trong luồng đầu vào được đặt thành 1024 sau giải mã đầu tiên. Vì vậy, tôi thêm inputstream.reset() trước giải mã thứ hai. Điều đó hoạt động. Hy vọng có thể giúp đỡ người khác.

+6

Điều này làm việc cho tôi, nhưng chắc chắn rằng bạn gọi '' 'boolean supported = inputStream.markSupported()' '' đầu tiên. Đánh dấu phải được hỗ trợ hoặc đặt lại() sẽ ném một '' 'IOException'''. Nếu nó không được hỗ trợ, có thể mở luồng đầu vào thứ hai. – Shellum

+0

bạn đã giải quyết vấn đề này bằng cách nào? tôi đã nhận được cùng một vấn đề và tôi đã đăng về nó ở đây: http://stackoverflow.com/questions/17774442/how-to-get-bitmap-information-and-then-decode-bitmap-from-internet-inputstream. vì một số lý do, nó không hoạt động trên một số trang web và tệp rất cụ thể. –

1

Tôi gặp sự cố tương tự khi đọc luồng từ Mục đích được trả về từ ứng dụng thư viện. inputstream.reset() ném một IOException, như Shellum đề cập ở trên, vì vậy tôi đã giải quyết nó bằng cách chỉ đóng luồng và mở lại nó một lần nữa. Đơn giản, và đã làm các trick.

+0

Tôi đã làm theo lời khuyên này bằng cách đặt vị trí dòng thành 0 (trong C#) – Gandalf458

11

Xin chào các bạn, tôi đã tìm kiếm giải pháp tốt nhất. như #jia George chỉ ra bạn shold thiết lập lại các inputstream sau khi giải mã đầu tiên, vấn đề là một số thiết lập lại thời gian không được hỗ trợ nhưng bạn có thể bọc inputstream bên trong một BufferedInputStream và điều này là tốt.

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

điều duy nhất phù hợp với tôi! – Ricardo

11

Điều này có thể là một trường hợp hiếm nhưng đối với những bạn sử dụng Picasso bạn sẽ thấy lỗi này nếu bạn cố gắng tải một hình ảnh từ URL nhưng URL không đề cập đến một hình ảnh.

Ví dụ:
http://www.google.ca
thay vì:
https://www.google.ca/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png

+0

Ngoài ra, một số URL có thể kết thúc bằng '.jpg',' .png', '.tif', v.v. nhưng không thực sự tham chiếu đến hình ảnh. Ví dụ: http://www.guoguiyan.com/data/out/96/69698096-high-resolution-wallpapers.jpg. – shoe

Các vấn đề liên quan