2014-10-11 3 views
1

Я пытаюсь загрузить изображение из URL и получает повернутЗагрузка изображение из Url получает Повернуто Android

Я искал ExifOrientation для изображения наступающего из URL, но получили не удалось.

мой изображение URL является http://goo.gl/Y52pTo

это изображение из iPhone камеры и она будет вращаться в устройстве Android

Я пытался это решение но не работает для URL-я думаю,

https://gist.github.com/9re/1990019

Итак, Как я могу представить изображение в исходной ориентации?

Спасибо заранее.

+0

'он получает повернут в андроиде device'. Нет. Не здесь, в браузере. И не в приложении «Галерея». – greenapps

ответ

0

Вот мой код, который я использовал, чтобы решить,

Прежде всего скачать изображения.

new GetFileTask(new FileDownloadListener() { 

       @Override 
       public void onFileDownload(String path) { 


       } 
      }).execute(image_url, "image_name"); 

public interface FileDownloadListener{ 
    public void onFileDownload(String path); 

} 


public class GetFileTask extends AsyncTask<String, Void, String> { 
private FileDownloadListener downloadListner; 

public GetFileTask(FileDownloadListener downloadListner) { 
    this.downloadListner = downloadListner; 
} 

@Override 
protected void onPostExecute(String path) { 
    super.onPostExecute(path); 
    // Log.e("FILE_DOWNLOAD", "DOWNLOAD end: " + 
    // System.currentTimeMillis()); 
    File f = new File(path); 
    if (f.exists()) { 
     downloadListner.onFileDownload(path); 
    } 
} 

/* 
* param 0 : url param 1 : file name without path 
*/ 
@Override 
protected String doInBackground(String... params) { 
    try { 
     // Log.e("FILE_DOWNLOAD", 
     // "DOWNLOAD Start: " + System.currentTimeMillis()); 
     File dir = new File(General.TempImagePath); 
     if (!dir.exists()) { 
      dir.mkdirs(); // create complete path require to create this 
          // directory 
     } 

     URL ulrn = new URL(params[0]); 
     HttpURLConnection con = (HttpURLConnection) ulrn.openConnection(); 
     InputStream is = con.getInputStream(); 
     // Log.e("FILE_DOWNLOAD", 
     // "DOWNLOAD InputStream: " + System.currentTimeMillis()); 

     String fileName = (params[1] == null) ? String.valueOf(System 
       .currentTimeMillis()) : params[1]; 

     try { 
      OutputStream fOut = null; 
      File file = new File(General.TempImagePath, fileName); 
      if (file.exists()) { 
       file.delete(); 
      } 

      fOut = new FileOutputStream(file); 

      int read = 0; 
      byte[] bytes = new byte[1024]; 

      while ((read = is.read(bytes)) != -1) { 
       fOut.write(bytes, 0, read); 
      } 
      // Log.e("FILE_DOWNLOAD", 
      // "DOWNLOAD file-read: " + System.currentTimeMillis()); 
      is.close(); 

      fOut.flush(); 
      fOut.close(); 

      return file.getAbsolutePath(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

} 

затем с помощью пути повернутся растровое изображение, как это,

public static Bitmap rotateBitmap(String src, Bitmap bitmap) { 
    try { 
     int orientation = getExifOrientation(src); 

     if (orientation == 1) { 
      return bitmap; 
     } 

     Matrix matrix = new Matrix(); 
     switch (orientation) { 
     case 2: 
      matrix.setScale(-1, 1); 
      break; 
     case 3: 
      matrix.setRotate(180); 
      break; 
     case 4: 
      matrix.setRotate(180); 
      matrix.postScale(-1, 1); 
      break; 
     case 5: 
      matrix.setRotate(90); 
      matrix.postScale(-1, 1); 
      break; 
     case 6: 
      matrix.setRotate(90); 
      break; 
     case 7: 
      matrix.setRotate(-90); 
      matrix.postScale(-1, 1); 
      break; 
     case 8: 
      matrix.setRotate(-90); 
      break; 
     default: 
      return bitmap; 
     } 

     try { 
      Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, 
        bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
      bitmap.recycle(); 
      return oriented; 
     } catch (OutOfMemoryError e) { 
      e.printStackTrace(); 
      return bitmap; 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return bitmap; 
} 

private static int getExifOrientation(String src) throws IOException { 
    int orientation = 1; 

    try { 
     /** 
     * if your are targeting only api level >= 5 ExifInterface exif = 
     * new ExifInterface(src); orientation = 
     * exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 
     */ 
     if (Build.VERSION.SDK_INT >= 5) { 
      Class<?> exifClass = Class 
        .forName("android.media.ExifInterface"); 
      Constructor<?> exifConstructor = exifClass 
        .getConstructor(new Class[] { String.class }); 
      Object exifInstance = exifConstructor 
        .newInstance(new Object[] { src }); 
      Method getAttributeInt = exifClass.getMethod("getAttributeInt", 
        new Class[] { String.class, int.class }); 
      Field tagOrientationField = exifClass 
        .getField("TAG_ORIENTATION"); 
      String tagOrientation = (String) tagOrientationField.get(null); 
      orientation = (Integer) getAttributeInt.invoke(exifInstance, 
        new Object[] { tagOrientation, 1 }); 
     } 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } catch (SecurityException e) { 
     e.printStackTrace(); 
    } catch (NoSuchMethodException e) { 
     e.printStackTrace(); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (InstantiationException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     e.printStackTrace(); 
    } catch (NoSuchFieldException e) { 
     e.printStackTrace(); 
    } 

    return orientation; 
} 
0

, если его происходит каждый раз, то вы можете повернуть изображение после его загрузки ....

+0

Спасибо ... за то, что дал мне идею. –

+0

Его не происходит каждый раз. это происходит для изображений, которые я снимаю с iPhone Camera и загружаю на сервер. –

Смежные вопросы