保存url到本地,刷新圖庫,解決保存圖片不顯示

//直接上代碼
GlideUtils.loadImageAndSave(mContext, imgUrl, new GlideUtils.Listener() {
                                        @Override
                                        public void OnSuccess(String path) {
                                            // 通知相冊有新圖片,不通知保存后會出現(xiàn)相冊不顯示的情況,做重啟之類操作會顯示,或做其他刷新媒體操作
                                            File file = new File(path);
                                            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                            Uri uri = Uri.fromFile(file);
                                            intent.setData(uri);
                                            ImagePagerActivity.this.sendBroadcast(intent);
                                        }

                                        @Override
                                        public void OnError() {

                                        }
                                    });
class GlideUtils{
/**
     * 加載并下載
     */
  public static void loadImageAndSave(Context context, String url, final Listener mListener) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.mipmap.head);
        options.diskCacheStrategy(DiskCacheStrategy.ALL);
        options.error(R.mipmap.head_defaul);
        try {
            Glide.with(context).load(url).apply(options).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    try {
                        savaBitmap(System.currentTimeMillis() + ".jpg", Bitmap2Bytes(drawableToBitmap(resource)), mListener);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }catch (Exception e){}
    }

  public static byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

  public interface Listener{
        void OnSuccess(String path);
        void OnError();
    }

// 保存圖片到手機指定目錄
    public static void savaBitmap(String imgName, byte[] bytes, Listener mListener) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String filePath = null;
            FileOutputStream fos = null;
            try {
                filePath = Environment.getExternalStorageDirectory().getCanonicalPath() + "/NewImg";
                File imgDir = new File(filePath);
                if (!imgDir.exists()) {
                    imgDir.mkdirs();
                }
                imgName = filePath + "/" + imgName;
                fos = new FileOutputStream(imgName);
                fos.write(bytes);
                Toast.makeText(context, "圖片已保存到" + filePath, Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Log.i("-----", "savaBitmap: =============" + imgName);
            mListener.OnSuccess(imgName);
            return;
        } else {
            Toast.makeText(context, "請檢查SD卡是否可用", Toast.LENGTH_SHORT).show();
        }
        mListener.OnError();
    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容