gpt4 book ai didi

java - 无法在 Android 中从 URL 加载照片

转载 作者:行者123 更新时间:2023-11-29 04:34:11 24 4
gpt4 key购买 nike

我正在开发一个壁纸应用程序。我写了一个 AsyncTask 从选定的 url 下载照片并将其加载为墙纸(全部在后台)。现在,AsyncTask 有时会起作用。而我自己无法理解无法从 URL 加载/下载图像(作为位图)的原因是什么。两个 url(好的和坏的)都在工作并在浏览器上加载正确的图像。

我的 AsyncTask 类:

@Override
protected Void doInBackground(String... params) {
Bitmap output = null;
try {
URL url = new URL(params[0]);
Log.d(TAG, "_log: output URL is: "+url.toString());

HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
output = BitmapFactory.decodeStream(inputStream);

Log.d(TAG, "_log: output size "+output.getByteCount());
} catch (MalformedURLException e) {
e.printStackTrace();
Log.d(TAG, "_log: output with MalformedURLException "+e.toString());
} catch (IOException e) {
Log.d(TAG, "_log: output with IOException "+e.toString());
e.printStackTrace();
} catch (Exception e) {
Log.d(TAG, "_log: output with Exception "+e.toString());
e.printStackTrace();
} finally {
if (output != null) util.setupWallpaper(context, output);
else Log.d(TAG, "_log: output is null");
}
return null;
}

setupWallpaper 函数是:

public void setupWallpaper(Context context, Bitmap image) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
try {
if (image == null) {
makeToast(context, "Image is null!");
} else {
wallpaperManager.setBitmap(image);
}
} catch (IOException e) {
e.printStackTrace();
}
}

错误只是在我尝试获取空对象的大小时输出时捕获到异常的 stackTrace。

output with Exception java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getByteCount()' on a null object reference

RetrieveFeed.doInBackground(RetrieveFeed.java:57)
RetrieveFeed.doInBackground(RetrieveFeed.java:27)

权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

我不是要将此图像加载到任何 imageView 中,而是将此图像发送到 WallPaperManager 以加载为墙纸。

我想指出的是,我用 Glide 尝试了同样的事情并且出现了同样的结果!

最佳答案

(代表 OP 发布)

问题是,错误的 url 图像太大而无法处理,因此 Bitmap 静默失败。

D/skia: --- too Large Progressive Image (1, 5184 x 3456)> limit(16777216)
D/skia: --- decoder->decode returned false

关于java - 无法在 Android 中从 URL 加载照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42441745/

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