0

I try to download image from url with help of picasso.But i cant do. i spend time in this more than two days. I tried async task that also failed. i tried alot. Please anyone help me.

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;
    }
}

Thanks in advance!

Jaxian
  • 1,146
  • 7
  • 14
hikoo
  • 517
  • 4
  • 10
  • 20
  • 2
    Possible duplicate of [Download and Save Images Using Picasso](http://stackoverflow.com/questions/27729976/download-and-save-images-using-picasso) – Janki Gadhiya Jul 02 '16 at 09:02
  • Thats different question.. – hikoo Jul 02 '16 at 09:04
  • the answers to that question provides the answer to your question too.. It is just that ListView is a access thing in that question.. But that does not matter as the main purpose is downloading the images..!! – Janki Gadhiya Jul 02 '16 at 09:07
  • Possible duplicate of http://stackoverflow.com/questions/22505290/preload-images-into-memory-disk-with-android-picasso – Denny Weinberg Jul 02 '16 at 09:34

1 Answers1

0

Try this:

// 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() {
        }
 });

Hope this will help you :)

AhmadReza Payan
  • 2,171
  • 1
  • 23
  • 33