2016-03-29 3 views
0

Ich versuche, durch folgenden Code Bild von URL in Android-Anwendung anzuzeigen:Anzeigen von Bildern, basierend auf EXIF-Daten von URL in android

img = (ImageView) view.findViewById(R.id.img); 

new LoadImage().execute("http://localhost" + file_name); 

Es funktioniert ziemlich gut, aber es EXIF-Daten eines Bildes ignoriert, so Mein Bild wird gedreht. Wie können Bilder abhängig von ihren EXIF-Daten angezeigt werden?

Antwort

1

Anruf fixOrientation zu beheben Ihre Bildausrichtung

public static int getExifRotation(String imgPath) { 
    try { 
     ExifInterface exif = new ExifInterface(imgPath); 
     String rotationAmount = exif 
       .getAttribute(ExifInterface.TAG_ORIENTATION); 
     if (!TextUtils.isEmpty(rotationAmount)) { 
      int rotationParam = Integer.parseInt(rotationAmount); 
      switch (rotationParam) { 
       case ExifInterface.ORIENTATION_NORMAL: 
        return 0; 
       case ExifInterface.ORIENTATION_ROTATE_90: 
        return 90; 
       case ExifInterface.ORIENTATION_ROTATE_180: 
        return 180; 
       case ExifInterface.ORIENTATION_ROTATE_270: 
        return 270; 
       default: 
        return 0; 
      } 
     } else { 
      return 0; 
     } 
    } catch (Exception ex) { 
     return 0; 
    } 
} 

public static Bitmap fixOrientation(String filePath, Bitmap bm) { 
    int orientation = getExifRotation(filePath); 
    if (orientation == 0 || orientation % 360 == 0) { 
     //it is already right orientation, no need to rotate 
     return bm; 
    } 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(orientation); 
    return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), 
      matrix, true); 
} 

Ich schlage vor, Sie moderne Bild loader wie Glide oder Fresko verwenden, anstatt das Bild direkt mit AsyncTask der Handhabung.

+0

Ich benutzte Glide. Vielen Dank. –

Verwandte Themen