gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:03:44 25 4
gpt4 key购买 nike

我在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com