1. 程式人生 > >Glide載入圖片並儲存到本地返回file,bitmap

Glide載入圖片並儲存到本地返回file,bitmap

不廢話,直接上程式碼

[java] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片
  1. import android.content.Context;  
  2. import android.content.Intent;  
  3. import android.graphics.Bitmap;  
  4. import android.net.Uri;  
  5. import com.baguanv.jinba.utils.Const;  
  6. import com.bumptech.glide.Glide;  
  7. import com.bumptech.glide.request.target.Target;  
  8. import java.io.File;  
  9. import java.io.FileNotFoundException;  
  10. import java.io.FileOutputStream;  
  11. import java.io.IOException;  
  12. /** 
  13.  * 圖片下載 
  14.  *  
  15.  */
  16. publicclass DownLoadImageService implements Runnable {  
  17.     private String url;  
  18.     private Context context;  
  19.     private ImageDownLoadCallBack callBack;  
  20.     private File currentFile;  
  21.     public DownLoadImageService(Context context, String url, ImageDownLoadCallBack callBack) {  
  22.         this.url = url;  
  23.         this.callBack = callBack;  
  24.         this.context = context;  
  25.     }  
  26.     @Override
  27.     publicvoid run() {  
  28.         File file = null;  
  29.         Bitmap bitmap = null;  
  30.         try
     {  
  31. //            file = Glide.with(context)
  32. //                    .load(url)
  33. //                    .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
  34. //                    .get();
  35.             bitmap = Glide.with(context)  
  36.                     .load(url)  
  37.                     .asBitmap()  
  38.                     .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)  
  39.                     .get();  
  40.             if (bitmap != null){  
  41.                 // 在這裡執行圖片儲存方法
  42.                 saveImageToGallery(context,bitmap);  
  43.             }  
  44.         } catch (Exception e) {  
  45.             e.printStackTrace();  
  46.         } finally {  
  47. //            if (file != null) {
  48. //                callBack.onDownLoadSuccess(file);
  49. //            } else {
  50. //                callBack.onDownLoadFailed();
  51. //            }
  52.             if (bitmap != null && currentFile.exists()) {  
  53.                 callBack.onDownLoadSuccess(bitmap);  
  54.             } else {  
  55.                 callBack.onDownLoadFailed();  
  56.             }  
  57.         }  
  58.     }  
  59.     publicvoid saveImageToGallery(Context context, Bitmap bmp) {  
  60.         // 首先儲存圖片
  61.         String File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();//注意小米手機必須這樣獲得public絕對路徑
  62.         String fileName = ”新建資料夾”;  
  63.        File appDir = new File(file ,fileName);  
  64.         if (!appDir.exists()) {  
  65.             appDir.mkdirs();  
  66.         }  
  67.         String fileName = System.currentTimeMillis() + ”.jpg”;  
  68.         currentFile = new File(appDir, fileName);  
  69.         FileOutputStream fos = null;  
  70.         try {  
  71.             fos = new FileOutputStream(currentFile);  
  72.             bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);  
  73.             fos.flush();  
  74.         } catch (FileNotFoundException e) {  
  75.             e.printStackTrace();  
  76.         } catch (IOException e) {  
  77.             e.printStackTrace();  
  78.         } finally {  
  79.             try {  
  80.                 if (fos != null) {  
  81.                     fos.close();  
  82.                 }  
  83.             } catch (IOException e) {  
  84.                 e.printStackTrace();  
  85.             }  
  86.         }  
  87.         // 其次把檔案插入到系統圖庫
  88. //        try {
  89. //            MediaStore.Images.Media.insertImage(context.getContentResolver(),
  90. //                    currentFile.getAbsolutePath(), fileName, null);
  91. //        } catch (FileNotFoundException e) {
  92. //            e.printStackTrace();
  93. //        }
  94.         // 最後通知相簿更新
  95.         context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,  
  96.                 Uri.fromFile(new File(currentFile.getPath()))));  
  97.     }  
  98. }  
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;

import com.baguanv.jinba.utils.Const;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 圖片下載
 * 
 */
public class DownLoadImageService implements Runnable {
    private String url;
    private Context context;
    private ImageDownLoadCallBack callBack;
    private File currentFile;
    public DownLoadImageService(Context context, String url, ImageDownLoadCallBack callBack) {
        this.url = url;
        this.callBack = callBack;
        this.context = context;
    }

    @Override
    public void run() {
        File file = null;
        Bitmap bitmap = null;
        try {
//            file = Glide.with(context)
//                    .load(url)
//                    .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
//                    .get();
            bitmap = Glide.with(context)
                    .load(url)
                    .asBitmap()
                    .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .get();
            if (bitmap != null){
                // 在這裡執行圖片儲存方法
                saveImageToGallery(context,bitmap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            if (file != null) {
//                callBack.onDownLoadSuccess(file);
//            } else {
//                callBack.onDownLoadFailed();
//            }
            if (bitmap != null && currentFile.exists()) {
                callBack.onDownLoadSuccess(bitmap);
            } else {
                callBack.onDownLoadFailed();
            }
        }
    }

    public void saveImageToGallery(Context context, Bitmap bmp) {
        // 首先儲存圖片
        String File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();//注意小米手機必須這樣獲得public絕對路徑
        String fileName = "新建資料夾";
       File appDir = new File(file ,fileName);
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        currentFile = new File(appDir, fileName);

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(currentFile);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // 其次把檔案插入到系統圖庫
//        try {
//            MediaStore.Images.Media.insertImage(context.getContentResolver(),
//                    currentFile.getAbsolutePath(), fileName, null);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }

        // 最後通知相簿更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.fromFile(new File(currentFile.getPath()))));
    }
}
[java] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片
  1. publicinterface ImageDownLoadCallBack {  
  2.     void onDownLoadSuccess(File file);  
  3.     void onDownLoadSuccess(Bitmap bitmap);  
  4.     void onDownLoadFailed();  
  5. }  
public interface ImageDownLoadCallBack {
    void onDownLoadSuccess(File file);
    void onDownLoadSuccess(Bitmap bitmap);

    void onDownLoadFailed();
}

[java] view plain copy print?在CODE上檢視程式碼片派生到我的程式碼片
  1. /** 
  2.      * 啟動圖片下載執行緒 
  3.      */
  4.     privatevoid onDownLoad(String url) {  
  5.         DownLoadImageService service = new DownLoadImageService(getApplicationContext(),  
  6.                 url,  
  7.                 new ImageDownLoadCallBack() {  
  8.                     @Override
  9.                     publicvoid onDownLoadSuccess(File file) {  
  10.                     }  
  11.                     @Override
  12.                     publicvoid onDownLoadSuccess(Bitmap bitmap) {  
  13.                         // 在這裡執行圖片儲存方法
  14.                         Message message = new Message();  
  15.                         message.what = MSG_VISIBLE;  
  16.                         handler.sendMessageDelayed(message, delayTime);  
  17.                     }  
  18.                     @Override
  19.                     publicvoid onDownLoadFailed() {  
  20.                         // 圖片儲存失敗
  21.                         Message message = new Message();  
  22.                         message.what = MSG_ERROR;  
  23.                         handler.sendMessageDelayed(message, delayTime);  
  24.                     }  
  25.                 });  
  26.         //啟動圖片下載執行緒
  27.         new Thread(service).start();  
  28.     }  
/**
     * 啟動圖片下載執行緒
     */
    private void onDownLoad(String url) {
        DownLoadImageService service = new DownLoadImageService(getApplicationContext(),
                url,
                new ImageDownLoadCallBack() {

                    @Override
                    public void onDownLoadSuccess(File file) {
                    }
                    @Override
                    public void onDownLoadSuccess(Bitmap bitmap) {
                        // 在這裡執行圖片儲存方法
                        Message message = new Message();
                        message.what = MSG_VISIBLE;
                        handler.sendMessageDelayed(message, delayTime);
                    }

                    @Override
                    public void onDownLoadFailed() {
                        // 圖片儲存失敗
                        Message message = new Message();
                        message.what = MSG_ERROR;
                        handler.sendMessageDelayed(message, delayTime);
                    }
                });
        //啟動圖片下載執行緒
        new Thread(service).start();
    }