Android 图像无法下载,无法使用毕加索库保存在本地存储中
我正在尝试使用毕加索图书馆下载图像,然后将图像保存到本地存储器中。 错误:在方法Android 图像无法下载,无法使用毕加索库保存在本地存储中,android,picasso,Android,Picasso,我正在尝试使用毕加索图书馆下载图像,然后将图像保存到本地存储器中。 错误:在方法getImage()的日志“inside getImage”之后,logcat正在显示onBitmapFailed(Drawable errorDrawable)的日志。 我只能找到将目标引用转换为强引用的示例。然而,这并不能解决问题 下面是我为下载和保存图像而编写的代码片段: import android.content.Context; import android.content.ContextWrapper;
getImage()
的日志“inside getImage”之后,logcat正在显示onBitmapFailed(Drawable errorDrawable)
的日志。
我只能找到将目标引用转换为强引用的示例。然而,这并不能解决问题
下面是我为下载和保存图像而编写的代码片段:
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.util.Log;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import java.io.File;
import java.io.FileOutputStream;
public class DownloadImage {
Context mContext;
Target target;
public void getImage(Context context) {
mContext = context;
String path = "/data/data/ops.com.imagefetcher/images";
Picasso.with(context)
.load("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png")
.into(getTarget(path));
Log.e("Download Image: ", "inside getImage()");
}
private Target getTarget(final String path) {
// ContextWrapper cw = new ContextWrapper(mContext);
// final File directory = cw.getDir(path, Context.MODE_PRIVATE); // path to /data/data/yourapp/app_imageDir
target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {
@Override
public void run() {
Log.e("Download Image: ", "inside run");
final File myImageFile = new File(Environment.getExternalStorageDirectory().getPath() + "/" + path, "example");
FileOutputStream ostream = null;
try {
Log.e("Download Image: ", "inside try");
myImageFile.createNewFile();
ostream = new FileOutputStream(myImageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ostream.close();
Log.e("Download Image: ", "inside finally");
} catch (Exception e) {
e.printStackTrace();
}
}
Log.e("Download Image: ", "image saved to >>>" + myImageFile.getAbsolutePath());
}
}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.e("Download Image: ", "error!");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
return target;
}
}
清单文件是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ops.com.imagefetcher">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"
android:enabled="true"
android:exported="true"/>
</application>
</manifest>
使用默认的
DownloadManager
下载图像
private long downloadFile( String uRl, String desc, String name) {
File direct = new File(Environment.getExternalStorageDirectory()
+ "/MyFolderName");
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mgr = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle(name)
.setDescription(desc)
.setVisibleInDownloadsUi(true)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalFilesDir(context,"/MyFolderName", name + ".jpg")
.setDestinationInExternalPublicDir("/MyFolderName", name + ".jpg");
return mgr.enqueue(request);
}
只需将url传递给此方法。它将完成所有必需的过程请发布准确的(复制粘贴)logcat错误和堆栈跟踪。对于errorDrawable,没有printStackTrace()
private long downloadFile( String uRl, String desc, String name) {
File direct = new File(Environment.getExternalStorageDirectory()
+ "/MyFolderName");
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mgr = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle(name)
.setDescription(desc)
.setVisibleInDownloadsUi(true)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalFilesDir(context,"/MyFolderName", name + ".jpg")
.setDestinationInExternalPublicDir("/MyFolderName", name + ".jpg");
return mgr.enqueue(request);
}