我正在我的应用中选择要用作用户图片的图片,如下所示:
Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, IMAGE_GALLERY);
在onActivityResult()
中if(requestCode == IMAGE_GALLERY && resultCode == RESULT_OK) {
Uri uri = intent.getData();
if(uri != null) {
this.picture = Utils.ScaleBitmap(context, uri, 640);
userPic.setScaleType(ImageView.ScaleType.CENTER_CROP);
userPic.setPadding(0,0,0,0);
userPic.setImageBitmap(picture);
}
}
我的Utils.ScaleBitmap方法如下:
try {
//Getting file path from URI
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(imageURI, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
//Getting EXIF info to rotate image
ExifInterface exif = null;
try {
File pictureFile = new File(picturePath);
exif = new ExifInterface(pictureFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
int orientation = ExifInterface.ORIENTATION_NORMAL;
if (exif != null)
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
bitmap = rotateBitmap(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap = rotateBitmap(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
bitmap = rotateBitmap(bitmap, 270);
break;
}
//Compressing image
int w = bitmap.getWidth(), h = bitmap.getHeight();
int width, height;
if (w > max || h > max) {
if (w == h) {
width = height = max;
} else if (w < h) {
height = max;
width = max * w / h;
} else {
width = max;
height = max * h / w;
}
} else {
width = w;
height = h;
}
//Bitmap bitmap = ((BitmapDrawable) commentImage.getDrawable()).getBitmap();
Bitmap scaledphoto = Bitmap.createScaledBitmap(bitmap, width, height, true);
return scaledphoto;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
问题在于,此代码不适用于我从云端选择的图片,例如Google云端硬盘,Picasa等。
当我没有做所有旋转的东西时,它曾经工作过。这只是
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageURI);
我可以挑选任何图片并且工作。但我的相机拍摄的图像方向错误。现在我纠正了旋转,但是无法从云中获取图像。
有谁知道我怎么能让这两件事都有效?
我的意思是,我希望能够从云存储中选择图像,并且还能够旋转方向错误的图像。
答案 0 :(得分:0)
您的问题是从云端获取图片。这已经得到了回答。 For an example look here
我仍然使用
获取位图 bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uri);
我也需要EXIF数据,下面是基于@paulburke代码的代码
/**
* 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.<br>
* <br>
* Callers should check whether the path is local before assuming it
* represents a local file.
*
* @param context The context.
* @param uri The Uri to query.
* @author paulburke
*/
public static String getPathFromURI(final Context context, final Uri uri) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Authority: " + uri.getAuthority() +
", Fragment: " + uri.getFragment() +
", Port: " + uri.getPort() +
", Query: " + uri.getQuery() +
", Scheme: " + uri.getScheme() +
", Host: " + uri.getHost() +
", Segments: " + uri.getPathSegments().toString());
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
} else if (isDownloadsDocument(uri)) {
// DownloadsProvider
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);
} else if (isMediaDocument(uri)) {
// MediaProvider
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);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
// MediaStore (and general)
String res = getDataColumn(context, uri, null, null);
// Return the remote address
if (res == null && isGooglePhotosUri(uri))
return uri.getLastPathSegment();
else
return res;
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
// File
return uri.getPath();
}
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.
* @author paulburke
*/
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.
* @author paulburke
*/
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.
* @author paulburke
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority()) ||
"com.google.android.apps.photos.contentprovider".equals(uri.getAuthority());
}
/**
* 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.
* @author paulburke
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
final String column = "_data";
final String[] projection = { column };
try (Cursor cursor =
context.getContentResolver().query(uri, projection, selection, selectionArgs, null)) {
if (cursor != null && cursor.moveToFirst()) {
if (BuildConfig.DEBUG)
DatabaseUtils.dumpCursor(cursor);
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
}
return null;
}
据我记得,主要的是找出他的isGooglePhotosUri()
方法