我尝试在picasso的帮助下从网址下载图片。但我不能这样做。我花了两天多的时间。我尝试过同步失败的异步任务。我尝试了很多。请有人帮助我。
protected static void postNotification(Intent intentAction, Context context,String msg,String url){
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL);
/* ImageDownloaderTask image = new ImageDownloaderTask();
image.execute(url);*/
Bitmap bitmapImage = retriveImage(url,context); //here only i want get that image.
Log.d("Bitmap", String.valueOf(bitmapImage));//Its print only null
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.tapn)
.setContentTitle("Welcome!")
.setContentText(msg)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmapImage))
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.getNotification();
mNotificationManager.notify(R.string.notification_number, notification);
}
private static Bitmap retriveImage(String url,Context c) {
CacheTarget cacheTarget = new CacheTarget();
Picasso.with(c)
.load(url)
.into(cacheTarget);
return cacheTarget.getCacheBitmap();
}
private static class CacheTarget implements Target {
private Bitmap cacheBitmap;
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
cacheBitmap = bitmap;
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
public Bitmap getCacheBitmap() {
return cacheBitmap;
}
}
提前致谢!
答案 0 :(得分:0)
试试这个:
// Define where you want to save the file
String dirName = Environment.getExternalStorageDirectory().toString() + "/myAppData/images";
final File storageDir = new File(dirName);
// Create directory
if (!storageDir.exists()) {
storageDir.mkdirs();
}
// Your image address. ex: "http://http://stackoverflow.com/myImages.jpg"
String MY_IMAGE_URL = "The image url that you wish to download";
final ImageView profile = new ImageView(activity);
final String img_path = storageDir.getAbsolutePath() + "/" + "myimage.jpg";
Picasso.with(activity).load(MY_IMAGE_URL).into(profile, new Callback() {
@Override
public void onSuccess() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// Save bitmap to local
Bitmap bitmap = ((BitmapDrawable)profile.getDrawable()).getBitmap();
File file = new File(img_path);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
},100);
}
@Override
public void onError() {
}
});
希望这会对你有所帮助:)。