2017-11-23 17 views
0

내 앱에서 사용자가 휴대 전화 저장소에서 이미지를 선택하고 표시하도록 허용하려고합니다. 그렇게하려고하면 이미지가 회전 된 상태로 돌아 오는 문제가 발생했습니다. 그래서 몇 가지 검색을 한 후 올바른 방법으로 코드 조각을 찾았습니다. 그러나 Google 드라이브 또는 클라우드 저장소에서 선택한 이미지에 대해 동일한 전략을 테스트하면 오류가 발생합니다.갤러리의 방향 고정을 위해 갤러리 대신 Google 드라이브에서 이미지의 절대 경로를 어떻게 가져올 수 있습니까?


는 여기에 내가 절대 경로를 얻을 및 이미지 회전을 수정하는 데 사용하는 코드 조각입니다.

private static String getRealPathFromURI(Context context, Uri uri) { 
    String filePath = ""; 
    String wholeID = DocumentsContract.getDocumentId(uri); 
    Log.d("here", wholeID); 
    // Split at colon, use second item in the array 
    String id = wholeID.split(":")[1]; 

    String[] column = { MediaStore.Images.Media.DATA }; 

    // where id is equal to 
    String sel = MediaStore.Images.Media._ID + "=?"; 

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
      column, sel, new String[]{ id }, null); 

    int columnIndex = cursor.getColumnIndex(column[0]); 

    if (cursor.moveToFirst()) { 
     filePath = cursor.getString(columnIndex); 
    } 
    cursor.close(); 
    Log.d("here", filePath); 
    return filePath; 
} 

    public static Bitmap modifyOrientation(Bitmap bitmap, String image_absolute_path) throws IOException { 
    ExifInterface ei = new ExifInterface(image_absolute_path); 
    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; 
    } 
} 

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

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

그리고 마지막으로는 비트 맵을받은 후, 여기에 수정 방법을 사용하고 있습니다 :

public static String encodeImgToBase64(Uri uri, Context context) throws IOException { 
    InputStream inputStream = context.getContentResolver().openInputStream(uri); 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options); 
    inputStream.close(); 
    inputStream = context.getContentResolver().openInputStream(uri); 
    options.inSampleSize = calculateInSampleSize(options, REQUIRED_WIDTH, REQUIRED_HEIGHT); 
    options.inJustDecodeBounds = false; 
    bitmap = BitmapFactory.decodeStream(inputStream, null, options); 
    inputStream.close(); 

    bitmap = modifyOrientation(bitmap, getRealPathFromURI(context, uri)); 

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream); 
    byte[] imgByteArray = byteArrayOutputStream.toByteArray(); 
    byteArrayOutputStream.close(); 
    return Base64.encodeToString(imgByteArray, Base64.DEFAULT); 
} 

그래서, 내 질문은 클라우드 스토리지에서 선택한 이미지에 같은 일을하는 방법은?

+0

'public static 비트 맵 modifyOrientation (비트 맵 비트 맵, 문자열 image_absolute_path)'. 비트 맵? 사용자가 Google 드라이브에서 선택한 것으로부터 어떻게 비트 맵을 얻었습니까? – greenapps

+0

다른 기능을 사용하고 있습니다. 필요합니까? –

+0

'사용자가 Google 드라이브에서 선택한 것으로부터 비트 맵을 어떻게 얻었습니까? ' 맙소사. 왜 두 번 물어봐야하니? – greenapps

답변

1

이미지를 다운로드하고 장치 tmp 저장소에 저장 한 다음 갤러리 이미지에 사용한 것과 같은 방법을 사용할 수 있습니다.

+0

사실 그것은 주위에 허용되는 작품이지만, 나는 하나 있다면 더 직접적인 방법을 선호합니다. 감사합니다. –