2012-08-23 2 views
1

내 안드로이드 애플 리케이션의 서버에서 다운로드 이미지에 문제가 있습니다. 내가 https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg파일 이름 결과가 안드로이드의 원격 서버에서 이미지를 다운로드하지 않습니다.

에서 이미지 다운로드하려고하면
이 이미지 다운로드하지 의미 널 (null)을 반환합니다. 이제 서버에서 파일 이름을 바꿔서 테스트하기 위해 다른 폴더 (https://www.morroccomethod.com/mm/12.jpg)에 복사하면 놀랍습니다. 이미지가 성공적으로 다운로드되었습니다.
파일의 평균 이름은 이미지를 다운로드하지 않음을 나타냅니다.

이제 누구나 나를 도와주고 안내하길 원합니다.

답변

1

아마도 이미지를 다운로드하는 올바른 방법을 사용하고 있지 않을 수 있습니다. 나는 그 문제를 정확히 알지 못한다. 그러나 나는 언제나 나를 위해 일하는 하나의 수업을 사용 해왔다.

ImageLoading.java

public class ImageLoading { 

public enum BitmapManager { 
    INSTANCE; 

    private final Map<String, SoftReference<Bitmap>> cache; 
    private final ExecutorService pool; 
    private Map<ImageView, String> imageViews = Collections 
      .synchronizedMap(new WeakHashMap<ImageView, String>()); 
    private Bitmap placeholder; 

    BitmapManager() { 
     cache = new HashMap<String, SoftReference<Bitmap>>(); 
     pool = Executors.newFixedThreadPool(5); 
    } 

    public void setPlaceholder(Bitmap bmp) { 
     placeholder = bmp; 
    } 


    public Bitmap getBitmapFromCache(String url) { 
     if (cache.containsKey(url)) { 
      return cache.get(url).get(); 
     } 

     return null; 
    } 

    public void queueJob(final String url, final ImageView imageView, 
      final int width, final int height) { 
     /* Create handler in UI thread. */ 
     final Handler handler = new Handler() { 
      @Override 
      public void handleMessage(Message msg) { 
       String tag = imageViews.get(imageView); 
       if (tag != null && tag.equals(url)) { 
        if (msg.obj != null) { 
         imageView.setImageBitmap((Bitmap) msg.obj); 
        } else { 
         imageView.setImageBitmap(placeholder); 
         Log.d(null, "fail " + url); 
        } 
       } 
      } 
     }; 

     pool.submit(new Runnable() { 
      @Override 
      public void run() { 
       final Bitmap bmp = downloadBitmap(url, width, height); 
       Message message = Message.obtain(); 
       message.obj = bmp; 
       Log.d(null, "Item downloaded: " + url); 

       handler.sendMessage(message); 
      } 
     }); 
    } 

    public void loadBitmap(final String url, final ImageView imageView, 
      final int width, final int height) { 
     imageViews.put(imageView, url); 
     Bitmap bitmap = getBitmapFromCache(url); 

     // check in UI thread, so no concurrency issues 
     if (bitmap != null) { 
      Log.d(null, "Item loaded from cache: " + url); 
      imageView.setImageBitmap(bitmap); 
     } else { 
      imageView.setImageBitmap(placeholder); 
      queueJob(url, imageView, width, height); 
     } 
    } 

    private Bitmap downloadBitmap(String url, int width, int height) { 
     try { 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL( 
        url).getContent()); 
      bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true); 
      cache.put(url, new SoftReference<Bitmap>(bitmap)); 
      return bitmap; 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 
} 

}

그리고 당신은 이런 식으로 원하는 목적지를 구현 ->

ImageLoading.BitmapManager.INSTANCE.loadBitmap("image_url", your_bitmapImage, width,height); 
+0

나는 또한 시도 코드 만 같은 여전히 문제. –

+0

2.2에서는 작동하지만 이후 버전에서는 작동하지 않습니다. –

+0

"알 수없는 호스트 예외"www.moroccomethod.com " – DbSethi

관련 문제