Android 下載網絡圖片保存到相冊



下載類,可以url下載到相冊,記得在清單加權限,6.0代碼動態加權限判斷,下載圖片要在子線程中下載,下載完後廣播更新相冊

在清單文件裏面添加權限:

    <!--網絡-->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 讀寫文件 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


下載的url的工具類:

 

public class DonwloadSaveImg {
    private static Context context;
    private static String filePath;
    private static Bitmap mBitmap;
    private static String mSaveMessage = "失敗";
    private final static String TAG = "PictureActivity";
    private static ProgressDialog mSaveDialog = null;
 
    public static void donwloadImg(Context contexts, String filePaths) {
        context = contexts;
        filePath = filePaths;
        mSaveDialog = ProgressDialog.show(context, "保存圖片", "圖片正在保存中,請稍等...", true);
        new Thread(saveFileRunnable).start();
    }
 
    private static Runnable saveFileRunnable = new Runnable() {
        @Override
        public void run() {
            try {
                if (!TextUtils.isEmpty(filePath)) { //網絡圖片
                    // 對資源鏈接
                    URL url = new URL(filePath);
                    //打開輸入流
                    InputStream inputStream = url.openStream();
                    //對網上資源進行下載轉換位圖圖片
                    mBitmap = BitmapFactory.decodeStream(inputStream);
                    inputStream.close();
                }
                saveFile(mBitmap);
                mSaveMessage = "圖片保存成功!";
            } catch (IOException e) {
                mSaveMessage = "圖片保存失敗!";
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            messageHandler.sendMessage(messageHandler.obtainMessage());
        }
    };
 
    private static Handler messageHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mSaveDialog.dismiss();
            Log.d(TAG, mSaveMessage);
            Toast.makeText(context, mSaveMessage, Toast.LENGTH_SHORT).show();
        }
    };
    /**
     * 保存圖片
     * @param bm
     * @throws IOException
     */
    public static void saveFile(Bitmap bm ) throws IOException {
        File dirFile = new File(Environment.getExternalStorageDirectory().getPath());
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }
        String fileName = UUID.randomUUID().toString() + ".jpg";
        File myCaptureFile = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + fileName);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
        //把圖片保存後聲明這個廣播事件通知系統相冊有新圖片到來
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(myCaptureFile);
        intent.setData(uri);
        context.sendBroadcast(intent);
    }
}


在Activity中進行調用:

public class DownloadSavePictureActivity extends AppCompatActivity {
    private Button btn_pic;
    private Bitmap bitmap;
    private String Path="http://39.106.39.112:33/MAB214ef69e6224117.png";
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_save_picture);
        btn_pic=findViewById(R.id.btn_pic);
        btn_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImgDonwloads.donwloadImg(DownloadSavePictureActivity.this,Path);//iPath
            }
        });
    }
}


 注意:對於Android6.0以上的手機一定要判斷相應的權限是否開啓

尊重作者成果:https://blog.csdn.net/yushuangping/article/details/84986691

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章