2

次のコードを使用して、電話のギャラリーから画像を取得しました。

Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_GET_CONTENT);
  startActivityForResult(Intent.createChooser(intent, "Select Picture"),
    USE_LIBRARY_PIC_REQUEST);

onActivityResult()で、ファイルパスが表示されます。ファイルパスを使用してビットマップを取得しようとしましたが、常に横向きで表示されます。その画像を常に縦向きで表示する方法はありますか?ファイルパスを取得するためのコード:

Uri selectedImageUri = data.getData();
    selectedImagePath = getPath(selectedImageUri);

これは私がファイルパスを使用してビットマップを取得した方法です:

Bitmap bmp=BitmapFactory.decodeFile(selectedImagePath);
4

1 に答える 1

7

元の画像を取得するには、このメソッドを使用できます。

public static Bitmap getBitmap(String uri, Context mContext) {

    Bitmap bitmap = null;
    Uri actualUri = Uri.parse(uri);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inTempStorage = new byte[16 * 1024];
    options.inSampleSize = 2;
    ContentResolver cr = mContext.getContentResolver();
    float degree = 0;
    try {
        ExifInterface exif = new ExifInterface(actualUri.getPath());
        String exifOrientation = exif
                .getAttribute(ExifInterface.TAG_ORIENTATION);
        bitmap = BitmapFactory.decodeStream(cr.openInputStream(actualUri),
                null, options);
        if (bitmap != null) {
            degree = getDegree(exifOrientation);
            if (degree != 0)
                bitmap = createRotatedBitmap(bitmap, degree);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

public static float getDegree(String exifOrientation) {
    float degree = 0;
    if (exifOrientation.equals("6"))
        degree = 90;
    else if (exifOrientation.equals("3"))
        degree = 180;
    else if (exifOrientation.equals("8"))
        degree = 270;
    return degree;
}

public static Bitmap createRotatedBitmap(Bitmap bm, float degree) {
    Bitmap bitmap = null;
    if (degree != 0) {
        Matrix matrix = new Matrix();
        matrix.preRotate(degree);
        bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                bm.getHeight(), matrix, true);
    }

    return bitmap;
}
于 2012-06-27T06:05:35.070 に答える