Android中图片保存到本地SD卡中

                //ImageView控件设为可保存
                mPhotoView.setDrawingCacheEnabled(true);
                Bitmap obmp = Bitmap.createBitmap(mPhotoView.getDrawingCache());
                mPhotoView.setDrawingCacheEnabled(false);
                //保存
                saveMyBitmap(getActivity(), obmp);

保存的方法:

//保存文件到指定路径
    public void saveMyBitmap(Context context, Bitmap bitmap) {
        String sdCardDir = Environment.getExternalStorageDirectory() + "/DCIM/";
        File appDir = new File(sdCardDir, "HappyBirthday");
        if (!appDir.exists()) {//不存在
            appDir.mkdir();
        }
        String fileName = "HappyBirthday" + System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(file);
        intent.setData(uri);
        getActivity().sendBroadcast(intent);
        Toast.makeText(getActivity(),"图片保存成功",Toast.LENGTH_SHORT).show();
    }

权限:

 
    
    

 

你可能感兴趣的:(图片)