2016-07-02 3 views
0

피카소의 도움으로 URL에서 이미지를 다운로드하려고합니다.하지만 할 수 없습니다. 나는 2 일 이상이 시간을 보낸다. 나는 또한 실패한 비동기 작업을 시도했다. 나는 많이 시도했다. 제발 도와주세요.피카소를 사용하여 URL에서 이미지를 다운로드하는 방법

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

미리 감사드립니다.

+2

가능한 중복 [다운로드 피카소를 사용하여 저장 이미지 (http://stackoverflow.com/questions/27729976/download- 도움이 될 것입니다 and-save-images-using-picasso) –

+0

다른 질문 .. – hikoo

+0

그 질문에 대한 답변도 귀하의 질문에 대한 답변을 제공합니다. 그것은 단지 ListView 그 질문에 액세스 할 수 있습니다 ..하지만 그건 중요하지 않습니다 주된 목적은 이미지를 다운로드하는 것입니다 .. !! –

답변

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

희망이 당신 :

관련 문제