如果图像的大小较小(例如100或200kb),则会下载该图像,但是如果图像的大小较大(如以MB为单位),则会显示已下载的消息,但内存中没有文件。
我的代码如下。
告诉我下面给出的代码中的错误
public static void setBgOrDownload(final String urlString, final Activity activity, final boolean download) {
Picasso.get().load(urlString).into(new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
if (download) {
saveImageBitmap(activity, urlString,bitmap);
} else {
new Thread(new Runnable() {
@Override
public void run() {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(activity);
try {
wallpaperManager.setBitmap(bitmap);
((PreviewActivity) activity).wallpaperSet(true);
} catch (IOException e) {
e.printStackTrace();
((PreviewActivity) activity).wallpaperSet(true);
}
}
}).start();
}
另一个代码
public static void saveImageBitmap(Activity context, String url, Bitmap bitmap) {
final AtomicBoolean isFilePresent = new AtomicBoolean(false);
String myDir = Environment.getExternalStorageDirectory().toString() + "/" + context.getResources().getString(R.string.app_name);
File fileDir = new File(myDir);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
url = url.substring(url.lastIndexOf("/") + 1);
final File newFile = new File(myDir, "Wallpaper_" + url);
File[] filesList = fileDir.listFiles();
if (filesList != null) {
ArrayList<File> files = new ArrayList<>(Arrays.asList(filesList));
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
Optional<File> result = files.stream().filter(new Predicate<File>() {
@Override
public boolean test(File file) {
return file.getAbsolutePath().equals(newFile.getAbsolutePath());
}
}).findFirst();
result.ifPresent(new Consumer<File>() {
@Override
public void accept(File file) {
isFilePresent.set(true);
}
});
} else {
for (File file : files) {
if (file.getAbsolutePath().equals(newFile.getAbsolutePath())) {
isFilePresent.set(true);
break;
}