2016-08-24 7 views
0

は、私はこのように私のアプリでは、ユーザーの画像として使用する画像を選択していて回転:onActivityResult()で[メディアの選択やクラウドからの画像とは、正しく

Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(pickPhoto, IMAGE_GALLERY); 

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を選ぶイメージで動作しない、ということですGoogleドライブ、Picasaなどのクラウドを利用してください。

私はすべての回転するものをやっていないときに働いていました。それだけでした

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

そして私はどんなイメージを選んでも働くことができました。しかし、私のカメラからの画像は間違った向きになっていました。今はローテーションを修正しましたが、クラウドから画像を取得することはできません。

どのように私は両方のことを動作させることができます知っていますか?

つまり、クラウドストレージから画像を選択できるようになり、間違った向きの画像を回転できるようになります。

+0

に解決策を見つけることでした "このコードは私が雲から選んだ画像では機能しませんe Googleドライブ、Picasaなど) - このコードには、クラウドに関連するものは何もないようです。あなたはローカルである 'MediaStore'から画像を選んでいます。どのようにクラウドから画像を選んでいますか? – CommonsWare

+0

ギャラリーを開くと、Picasaフォルダが表示されます。 Photosアプリでインテントを実行すると私はphotos.google.comの写真を選択できます。したがって、ACTION_PICKはローカルピクチャだけではありません。 –

+0

'File pictureFile = new File(picturePath)' - これは 'file'スキームを持つ' Uri'に対してのみ機能します。おそらく、あなたは 'content'のような異なるスキームを得ているでしょう。 PicassoやGlideなどのライブラリを使用する開発者は、画像の回転、スケーリングなどをライブラリの一部として取得します。このようなライブラリを使用したくない場合は、[ファイルではなくストリームで動作する 'ExifInterface'を使用してください(https://commonsware.com/blog/2016/05/31/tale-two-exifinterfaces.html )。 – CommonsWare

答えて

0

あなたの問題は、雲から画像を取得することです。これは前に答えられました。 For an example look here

私はまだ私も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; 
} 

に基づいて私のコードです

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

でビットマップを取得します主なものは、自分のisGooglePhotosUri()メソッド

+0

私はすでにそれを試みていて、クラウドイメージのために働いていませんでした。 –

+0

その答えはgithubの彼のプロジェクトを参照して、私はそこからコードを使用しました、それはいくつかのマイナーな調整が必要でしたが、最終的に私のために働いた。 –

+0

あなたがしなければならない調整を​​私に説明できますか? –

関連する問題