android - 文件名结果不从android中的远程服务器下载图像

标签 android imagedownload

我在我的 Android 应用程序中从服务器下载图像时遇到问题。
如果我尝试从 https://www.morroccomethod.com/components/com_virtuemart/shop_image/category/resized/Trial_Sizes_4e4ac3b0d3491_175x175.jpg下载图片

返回null表示图片未下载。 现在,当我替换服务器上的文件名并将其复制到其他文件夹(https://www.morroccomethod.com/mm/12.jpg)只是为了测试然后图像成功下载时,我感到很惊讶。
这意味着不下载图像的文件名。

现在我希望任何人都能帮助和指导我。

最佳答案

也许您没有使用正确的方法下载图片。我不完全知道其中的问题。但我一直使用一门课,每次都对我有用。

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);

关于android - 文件名结果不从android中的远程服务器下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12085234/

相关文章:

android - JQuery .on() 方法未绑定(bind)到 Android 4.0 Phonegap 应用程序中的所有元素

iphone - 关于发布到Appstore/Android Market的问题

Android - picasso 有时会错过照片

ASP.NET 可下载图像文件?

android - 在钛 map 上绘制路线

android - 多个 IntentService 或一个 Service

android - Gradle 在任务 transformClassesAndResourcesWithSyncLibJarsForDebug 中失败

java - Android从服务器下载图像并保存到sdcard而不使用BitmapFactory

iphone - UIWebView 的属性 scalesPageToFit = YES 在较大的图像上无法正常工作

ios - 在iOS中下载之前如何显示预览图像?