當我從圖庫中選擇圖像並將其設置在圖像視圖中時。它變成旋轉。我想要它的默認方向。如何將圖像旋轉到默認方向,從Android中的圖庫中選擇?
回答
它適用於我(從兩個不同的來源組合這個代碼)來解決從圖庫中選擇圖像旋轉。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
//..First convert the Image to the allowable size so app do not throw Memory_Out_Bound Exception
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
int resolution = 500;
options.inSampleSize = calculateInSampleSize(options, resolution , resolution);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
//...Now You have the 'bitmap' to rotate....
//...Rotate the bitmap to its original Orientation...
Bitmap bitmapNew = ImageOrientation.modifyOrientation(getApplicationContext(),bitmap,uri);
//...After Rotation set the image to Image View...
imageViewProfile.setImageBitmap(bitmapNew);
} catch (Exception e) {
Log.d("Image_exception",e.toString());
}
}
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight/inSampleSize) >= reqHeight
&& (halfWidth/inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
ImageOrientation類is..You的源代碼有任何你需要從活動或者只調用modifyOrientation()方法。
public class ImageOrientation {
public static Bitmap modifyOrientation(Context context,Bitmap bitmap,Uri uri) throws IOException {
ExifInterface ei = new ExifInterface(getPath(context,uri));
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotate(bitmap, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotate(bitmap, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotate(bitmap, 270);
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
return flip(bitmap, true, false);
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
return flip(bitmap, false, true);
default:
return bitmap;
}
}
private static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
private static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
private static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
private static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
private static Bitmap rotate(Bitmap bitmap, float degrees) {
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
private static Bitmap flip(Bitmap bitmap, boolean horizontal, boolean vertical) {
Matrix matrix = new Matrix();
matrix.preScale(horizontal ? -1 : 1, vertical ? -1 : 1);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
}
您確實找到了解決方案!大! – AndiGeeky
試試這個解決方案,並標記爲幫助完整,以便其他人可以得到它 –
從畫廊/攝像機選擇後獲得正確orientaion:
ExifInterface exifInterface = new ExifInterface(photoPath);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotateImage(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotateImage(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotateImage(bitmap, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
break;
}
public Bitmap rotateImage(Bitmap source, float angle) {
Matrix mat = new Matrix();
mat.postRotate(angle);
Bitmap bitmap= Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), mat,
return bitmap;
}
這需要角度... 但我解答我的答案是使圖片爲默認,它是完整的代碼從選擇設置圖像視圖 –
nightcoder ,他只需要以正確的方向獲得圖像,我回答。我不知道你爲什麼要在這裏放置整個onActivityResult方法。另外,它不需要手動角度代碼會自動檢測,你想通過你的評論告訴我什麼? –
阿倫庫馬爾,不要窮盡。我沒有說你的答案是不正確的! –
從圖庫中選擇時,爲了解決圖像旋轉,我已經使用這個和它的作品對我來說。
SaveCompressImage mSaveCompressImage = new SaveCompressImage(getApplicationContext());
String path = mSaveCompressImage.compressImage(imageFilePath);
SaveCompressImage.java
public class SaveCompressImage {
Context context;
public SaveCompressImage(Context context){
this.context=context;
}
public String compressImage(String imageUri) {
String filename=null ;
try {
String filePath = getRealPathFromURI(imageUri) ;
Bitmap scaledBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
// by setting this field as true, the actual bitmap pixels are not
// loaded in the memory. Just the bounds are loaded. If
// you try the use the bitmap here, you will get null.
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;
// max Height and width values of the compressed image is taken as
// 816x612
float maxHeight = 816.0f;
float maxWidth = 612.0f;
float imgRatio = (float)actualWidth/actualHeight;
float maxRatio = maxWidth/maxHeight;
// width and height values are set maintaining the aspect ratio of the
// image
if (actualHeight > maxHeight || actualWidth > maxWidth) {
if (imgRatio < maxRatio) {
imgRatio = maxHeight/actualHeight;
actualWidth = (int) (imgRatio * actualWidth);
actualHeight = (int) maxHeight;
} else if (imgRatio > maxRatio) {
imgRatio = maxWidth/actualWidth;
actualHeight = (int) (imgRatio * actualHeight);
actualWidth = (int) maxWidth;
} else {
actualHeight = (int) maxHeight;
actualWidth = (int) maxWidth;
}
}
// setting inSampleSize value allows to load a scaled down version of
// the original image
options.inSampleSize = calculateInSampleSize(options, actualWidth,
actualHeight);
// inJustDecodeBounds set to false to load the actual bitmap
options.inJustDecodeBounds = false;
// this options allow android to claim the bitmap memory if it runs low
// on memory
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = new byte[16 * 1024];
try {
// load the bitmap from its path
bmp =BitmapFactory.decodeFile(filePath, options);
} catch (OutOfMemoryError exception) {
}
try {
scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight,
Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError exception) {
}
if(scaledBitmap!=null){
float ratioX = actualWidth/(float) options.outWidth;
float ratioY = actualHeight/(float) options.outHeight;
float middleX = actualWidth/2.0f;
float middleY = actualHeight/2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bmp, middleX - bmp.getWidth()/2,
middleY - bmp.getHeight()/2, new Paint(
Paint.FILTER_BITMAP_FLAG));
// check the rotation of the image and display it properly
ExifInterface exif;
try {
exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Log.d("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 3) {
matrix.postRotate(180);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 8) {
matrix.postRotate(270);
Log.d("EXIF", "Exif: " + orientation);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
true);
} catch (IOException ioExceptione) {
}
FileOutputStream out = null;
filename = getFilename();
try {
out = new FileOutputStream(filename);
// write the compressed bitmap at the destination specified by
// filename.
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
} catch (FileNotFoundException fileNotFoundException) {
}
}
} catch (Exception exception) {
}
return filename;
}
public String getFilename() {
File file = new File(Constants.IMAGE_STORE_FOLDER);
if (!file.exists()) {
file.mkdirs();
}
String uriSting = (Constants.IMAGE_STORE_FOLDER
+ System.currentTimeMillis() + ".jpg");
return uriSting;
}
public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height
/(float) reqHeight);
final int widthRatio = Math.round((float) width/(float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
final float totalPixels = (float)width * height;
final float totalReqPixelsCap = (float)reqWidth * reqHeight * 2;
while (totalPixels/(inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
return inSampleSize;
}
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @author paulburke
*/
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
boolean a=DocumentsContract.isDocumentUri(context, uri);
// DocumentProvider
if (isKitKat) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
public String getRealPathFromURI(String contentURI) {
Uri contentUri = Uri.parse(contentURI);
Cursor cursor = context.getContentResolver().query(contentUri,
null, null, null, null);
if (cursor == null) {
return contentUri.getPath();
} else {
cursor.moveToFirst();
int index = cursor
.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
String pathh=cursor.getString(index);
return pathh;
}
}
}
- 1. 如何獲取從默認圖像庫中選擇的圖像的正確方向
- 2. Android從默認圖庫中選擇圖片?
- 3. 從圖庫中選擇圖像到imageview
- 4. 如何在Android中從圖庫或相機中選擇圖像?
- 5. 如何在Android中從圖庫中選擇多個圖像?
- 6. 如何以編程方式選擇默認縮略圖圖像?
- 7. UImagePicker從圖庫中選擇圖像,如何將圖庫滾動到底部?
- 8. 從圖庫中選擇的Android圖像方向始終爲0:Exif TAG
- 9. 如何從圖庫中選擇多個圖像Android
- 10. 從圖庫中選擇圖像視圖
- 11. 如何在Android中旋轉圖像?
- 12. 圖像獲取旋轉雖然方向爲0使用exif從圖庫中選擇
- 13. 如何在兩個方向旋轉圖像的Android
- 14. Android:旋轉位圖一次或使用EXIF方向旋轉圖像視圖
- 15. 如何在用戶選擇圖像時替換默認圖像?
- 16. 如何做圖庫圖像交換從左到右默認
- 17. 如何從javafx中的圖庫中選擇圖像?
- 18. 如何從Xamarin.ios中的圖庫中選擇圖像
- 19. 從圖庫中選擇多個圖像
- 20. 從圖庫中選擇多個圖像
- 21. 將圖像設置爲圖庫視圖而不從圖庫中選擇圖像
- 22. 如何旋轉圖庫視圖中的圖像
- 23. Android kitkat從圖庫中選擇圖片
- 24. Android Studio - 從圖庫中選擇圖片
- 25. 從媒體庫中選擇圖像時,默認爲「瀏覽」選項卡?
- 26. 將圖像旋轉到MousePos?
- 27. 如何從圖庫中選擇圖片?
- 28. 旋轉圖像,以特定的方向
- 29. Android旋轉圖像
- 30. Android旋轉圖像
詳細講解。到目前爲止你做了什麼!! – AndiGeeky
在此處添加您的代碼,顯示您如何編寫代碼! – AndiGeeky
我搜索了一個解決方案,我想與他人分享......作爲我的問題的回答 –