POPMUISE
在android Q及以下verison中保存捕获位图private class AsyncTaskExample extends AsyncTask<String, String, Bitmap> { Bitmap icon = null; ByteArrayOutputStream bytes; @Override protected void onPreExecute() { super.onPreExecute(); mBitmap = BaseApplication.getInstance().getBitmap(); ((BaseActivity)mContext).showFullScreenProgressLoader(((BaseActivity)mContext), ((BaseActivity)mContext).getString(R.string.please_wait), ((BaseActivity)mContext).getString(R.string.we_are_processing_your_request)); } @Override protected Bitmap doInBackground(String... strings) { try { String imageFileName = "picture_" + System.currentTimeMillis() + ".jpg"; File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + Constants.INTERNAL_FOLDER_NAME + "/"); directory.mkdirs(); mFile = new File(directory, imageFileName); if (!mFile.isFile()) { try { mFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) { final ContentResolver resolver = ((CameraFaceDetectionActivity) mContext).getContentResolver(); insertImage(resolver, mBitmap, mFile.getAbsolutePath(), ""); } else { FileOutputStream out = new FileOutputStream(mFile); try {// mBitmap = modifyOrientation(mBitmap,mFile.getAbsolutePath()); OutputStream output = new FileOutputStream(mFile); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, output); output.flush(); // Not really required output.close(); } catch (IOException e) { e.printStackTrace(); } } } catch(Exception e){ e.printStackTrace(); } return mBitmap; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { try { OutputStream output = new FileOutputStream(mFile); Bitmap b = modifyOrientation(mBitmap, mFile.getAbsolutePath()); b.compress(Bitmap.CompressFormat.JPEG, 100, output); } catch (IOException e) { e.printStackTrace(); } startActivity(new Intent(mContext, SelfiePreviewForgetPsdActivity.class) .putExtra(Constants.KEY_MOBILE, mMobileNumber) .putExtra(Constants.KEY_COUNTRY_CODE, mCountryCode) .putExtra(KEY_ID_NUMBER, idNumber) .putExtra(Constants.KEY_USER_DETAIL, mVerifyUserDetailResponse) .putExtra(KEY_ID_TYPE, idType) .putExtra(Constants.IMAGE_PATH, mFile.getAbsolutePath())); } }, 500); }}下面是在android Q上保存位图指定文件夹的方法public static String insertImage(ContentResolver cr, Bitmap source, String title, String description) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, title); values.put(MediaStore.Images.Media.DISPLAY_NAME, title); values.put(MediaStore.Images.Media.DESCRIPTION, description); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg"); // Add the date meta data to ensure the image is added at the front of the gallery values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis()); values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); Uri url = null; String stringUrl = null; /* value to be returned */ try { url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); if (source != null) { OutputStream imageOut = cr.openOutputStream(url); try { source.compress(Bitmap.CompressFormat.JPEG, 100, imageOut); } finally { imageOut.close(); } long id = ContentUris.parseId(url); // Wait until MINI_KIND thumbnail is generated. Bitmap miniThumb = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null); // This is for backward compatibility. storeThumbnail(cr, miniThumb, id, source.getWidth(), source.getHeight(), MediaStore.Images.Thumbnails.MICRO_KIND); } else { cr.delete(url, null, null); url = null; } } catch (Exception e) { if (url != null) { cr.delete(url, null, null); url = null; } } if (url != null) { stringUrl = url.toString(); } return stringUrl;}保存位图方法 private static final Bitmap storeThumbnail( ContentResolver cr, Bitmap source, long id, float width, float height, int kind) { // create the matrix to scale it Matrix matrix = new Matrix(); float scaleX = width / source.getWidth(); float scaleY = height / source.getHeight(); matrix.setScale(scaleX, scaleY); Bitmap thumb = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true ); ContentValues values = new ContentValues(4); values.put(MediaStore.Images.Thumbnails.KIND, kind); values.put(MediaStore.Images.Thumbnails.IMAGE_ID, (int) id); values.put(MediaStore.Images.Thumbnails.HEIGHT, thumb.getHeight()); values.put(MediaStore.Images.Thumbnails.WIDTH, thumb.getWidth()); Uri url = cr.insert(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, values); try { OutputStream thumbOut = cr.openOutputStream(url); thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut); thumbOut.close(); return thumb; } catch (Exception ex) { return null; }}