如题,目前只在三星手机上出现
解决方法:
- 先根据图片路径获取图片的旋转角度
/** * 获取图片的旋转角度 */ private static int getPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; }
- 如果旋转角度不为0,表示经过旋转了,调整过来即可
/** * 旋转图片 * 若图片旋转了就将其旋转回0° * @param img * @param degree * @return */ public static Bitmap toturn(Bitmap img, int degree) { Matrix matrix = new Matrix(); matrix.postRotate(+degree); int width = img.getWidth(); int height = img.getHeight(); img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true); return img; }
- 裁剪图片方法实现
/** * 裁剪图片方法实现 */ private static void startPhotoZoom(Activity activity, Uri uri) { //针对三星手机拍照旋转的问题 int degree; String filePath = UriUtils.getFileAbsolutePath(activity, uri); if ((degree = getPictureDegree(filePath)) != 0) { Bitmap mBitmap = null; try { mBitmap = getBitmapFormUri(activity, uri); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Bitmap nBitmap = toturn(mBitmap, degree); String tFilename = "t_" + System.currentTimeMillis()+".png"; String picPath = path + tFilename; try { BitmapUtils.save(nBitmap, picPath); } catch (IOException e) { e.printStackTrace(); } if (isSdk24()) { uri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileprovider", new File(picPath)); } else { uri = Uri.fromFile(new File(picPath)); } } int outputX = 350; int outputY = 350; File file = new File(path); if (!file.exists()) { file.mkdir(); } filename = System.currentTimeMillis()+".png"; Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // crop=true是设置在开启的Intent中设置显示的VIEW可裁剪 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", outputX); intent.putExtra("aspectY", outputY); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path + filename))); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); // 去黑边 // intent.putExtra("scale", true); // intent.putExtra("scaleUpIfNeeded", true); // 关闭人脸识别 // intent.putExtra("noFaceDetection", true); // intent.putExtra("outputFormat", // Bitmap.CompressFormat.PNG.toString()); //需要加上这两句话 : Uri 权限(重要,因为这个调试了两天) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); activity.startActivityForResult(intent, REQUEST_CODE_CROP); }
- 根据图片uri获取图片bitmap数据时,可能因为图片太大导致内存溢出问题,需要进行缩放和压缩
/** * 通过uri获取图片并进行压缩 * * @param uri */ public static Bitmap getBitmapFormUri(Activity ac, Uri uri) throws FileNotFoundException, IOException { InputStream input = ac.getContentResolver().openInputStream(uri); BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); onlyBoundsOptions.inJustDecodeBounds = true; onlyBoundsOptions.inDither = true;//optional onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional BitmapFactory.decodeStream(input, null, onlyBoundsOptions); input.close(); int originalWidth = onlyBoundsOptions.outWidth; int originalHeight = onlyBoundsOptions.outHeight; if ((originalWidth == -1) || (originalHeight == -1)) return null; //图片分辨率以屏幕大小为标准 float hh = ScreenUtils.getScreenHeight();//这里设置高度为屏幕高度 float ww = ScreenUtils.getScreenWidth();//这里设置宽度为屏幕宽度 //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if (originalWidth > originalHeight && originalWidth > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (originalWidth / ww); } else if (originalWidth < originalHeight && originalHeight > hh) {//如果高度高的话根据宽度固定大小缩放 be = (int) (originalHeight / hh); } if (be <= 0) be = 1; //比例压缩 BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = be;//设置缩放比例 bitmapOptions.inDither = true;//optional bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional input = ac.getContentResolver().openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions); input.close(); return compressImage(bitmap);//再进行质量压缩 } /** * 质量压缩方法 * * @param image * @return */ public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; while (baos.toByteArray().length / 1024 > 1000) { //循环判断如果压缩后图片是否大于1000kb,大于继续压缩 baos.reset();//重置baos即清空baos //第一个参数 :图片格式 ,第二个参数: 图片质量,100为最高,0为最差 ,第三个参数:保存压缩后的数据的流 image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中 options -= 10;//每次都减少10 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片 return bitmap; }
- 问题解决