图片保存

```java public class ImgUtils { //保存文件到指定路径 public static boolean saveImageToGallery(Context context, Bitmap bmp) { // 首先保存图片 String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dearxy"; File appDir = new File(storePath); if (!appDir.exists()) { appDir.mkdir(); } String fileName = System.currentTimeMillis() + ".jpg"; File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); //通过io流的方式来压缩保存图片 boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos); fos.flush(); fos.close(); //把文件插入到系统图库 //MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null); //保存图片后发送广播通知更新数据库 Uri uri = Uri.fromFile(file); context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); if (isSuccess) { return true; } else { return false; } } catch (IOException e) { e.printStackTrace(); } return false; } } ``` ```java public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks { private static final int REQUEST_CODE_SAVE_IMG = 10; private static final String TAG = "MainActivity"; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; findViewById(R.id.save_img_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { requestPermission(); } }); } /** * 请求读取sd卡的权限 */ private void requestPermission() { if (Build.VERSION.SDK_INT >= 23) { //读取sd卡的权限 String[] mPermissionList = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}; if (EasyPermissions.hasPermissions(mContext, mPermissionList)) { //已经同意过 saveImage(); } else { //未同意过,或者说是拒绝了,再次申请权限 EasyPermissions.requestPermissions( this, //上下文 "保存图片需要读取sd卡的权限", //提示文言 REQUEST_CODE_SAVE_IMG, //请求码 mPermissionList //权限列表 ); } } else { saveImage(); } } //保存图片 private void saveImage() { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.fj); boolean isSaveSuccess = ImgUtils.saveImageToGallery(mContext, bitmap); if (isSaveSuccess) { Toast.makeText(mContext, "保存图片成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(mContext, "保存图片失败,请稍后重试", Toast.LENGTH_SHORT).show(); } } //授权结果,分发下去 @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); // Forward results to EasyPermissions //跳转到onPermissionsGranted或者onPermissionsDenied去回调授权结果 EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); } //同意授权 @Override public void onPermissionsGranted(int requestCode, List list) { Log.i(TAG, "onPermissionsGranted:" + requestCode + ":" + list.size()); saveImage(); } //拒绝授权 @Override public void onPermissionsDenied(int requestCode, List perms) { Log.i(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size()); if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { //打开系统设置,手动授权 new AppSettingsDialog.Builder(this).build().show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) { //拒绝授权后,从系统设置了授权后,返回APP进行相应的操作 Log.i(TAG, "onPermissionsDenied:------>自定义设置授权后返回APP"); saveImage(); } } } ```

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