6

setRotation methodinCamera.Parametersは、すべてのデバイスで機能するわけではありません。EXIF問題を解決するには、情報を手動で変更することを提案する人がいます。画像の向きを縦に設定するような方法でexif情報を設定する方法の簡単な例を教えてください。ExifInterface

private int savePicture(byte[] data)
{
       File pictureFile = getOutputMediaFile();
       if (pictureFile == null)
           return FILE_CREATION_ERROR;

       try {
           FileOutputStream fos = new FileOutputStream(pictureFile);
           fos.write(data);
           fos.close();
       } catch (FileNotFoundException e) {
           return FILE_NOT_FOUND;
       } catch (IOException e) {
           return ACCESSING_FILE_ERROR;
       }

   return OKAY;
}

私はこれで試しました:

    try {
        ExifInterface exifi = new ExifInterface(pictureFile.getAbsolutePath());
        exifi.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_90));
        exifi.saveAttributes();
    } catch (IOException e) {
        Log.e(TAG, "Exif error");
    }

Androidギャラリーの写真を視覚化しても何も変わりません。

4

3 に答える 3

3

向きがファイルに保存されているのにギャラリーに表示されない場合は、向きが MediaStore にキャッシュされていることが原因である可能性があります。したがって、この情報もそこで更新する必要があります。

これが抜粋されたコードです(テストされていません)

/**
 * @param fileUri the media store file uri
 * @param orientation in degrees 0, 90, 180, 270
 * @param context
 * @return
 */
public boolean setOrientation(Uri fileUri, int orientation, Context context) {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.ORIENTATION, orientation);
    int rowsUpdated = context.getContentResolver().update(fileUri, values, null, null);
    return rowsUpdated > 0;
}

/**
 * Get content uri for the file path
 * 
 * @param path
 * @param context
 * @return
 */
public Uri getContentUriForFilePath(String path, Context context) {
    String[] projection = {
        MediaStore.Images.Media._ID
    };
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
            MediaStore.Images.Media.DATA + " = ?", new String[] {
                path
            }, null);
    Uri result = null;
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                long mediaId = cursor.getLong(0);
                result = ContentUris.withAppendedId(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mediaId);
            }
        } finally {
            cursor.close();
        }
    }
    return result;
}
于 2014-03-13T06:59:46.057 に答える
1

さて、私はそのような問題に行き詰まり、あなたが取り組んでいる解決策を試してみて、Matrix 関数を使用することになりました。

私が撮っていた画像が何であれ、それはすべて横向きだったので、アプリで以下のコードを適用しただけで、うまくいきました:-

Matrix matrix = new Matrix();
//set image rotation value to 90 degrees in matrix.
matrix.postRotate(90);
//supply the original width and height, if you don't want to change the height and width of //bitmap.
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true);
于 2013-11-03T15:27:04.583 に答える