2016-08-24 44 views
0

我選擇圖像在我的應用程序爲用戶的圖片使用這樣的:從媒體或雲,選擇圖像和正確輪播

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; 
} 

問題是,此代碼不適用於我選擇的圖像f rom雲,如谷歌驅動器,Picasa等

它曾經工作,當我沒有做所有的旋轉的東西。這只是

Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageURI); 

而且我可以選擇任何圖像和工作。但是我的相機拍攝的圖像出錯了。現在我糾正了旋轉,但無法從雲端獲取圖像。

有誰知道我怎麼能得到這兩種工作?

我的意思是,我希望能夠從雲存儲中選擇圖像,並且還能夠旋轉方向錯誤的圖像。

+0

「這段代碼不適用於我從雲端選取的圖像,lik e Google Drive,Picasa等「 - 此代碼中沒有涉及雲的任何內容。您正在從'MediaStore'中挑選圖片,這將是本地的。你如何從雲中選擇圖片? – CommonsWare

+0

當我打開圖庫時,會出現一個Picasa文件夾。如果我用Photos應用程序執行這個意圖。我可以選擇我的photos.google.com圖片。所以,ACTION_PICK不僅適用於本地圖片。 –

+0

'文件圖片文件=新文件(圖片路徑)' - 這隻適用於具有'文件'方案的'Uri'。也許你正在獲得不同的方案,如「內容」。使用Picasso或Glide等庫的開發人員可以獲得圖像旋轉,縮放以及所有這些作爲庫的一部分。如果你不想使用這樣的庫,可以使用[一個ExifInterface來處理流而不是文件](https://commonsware.com/blog/2016/05/31/tale-two-exifinterfaces.html )。 – CommonsWare

回答

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()方法的修復方法

+0

我已經試過,並沒有爲雲圖像工作。 –

+0

這個答案指的是他在github上的項目,我用了那裏的代碼,它需要一些小的調整,但最終爲我工作。 –

+0

你能向我解釋一下你必須做的調整嗎? –