2016-10-16 2 views
4

Ich habe Probleme beim Ausfindigmachen einer Ausnahme, die vom ExifInterface-Konstruktor ausgelöst wird, wenn versucht wird, die Exif-Schnittstelleninstanz mit einem Dateipfad zu initialisieren.ExifInterface-Konstruktor löst IOExxception

AKTUALISIERT Bitte beachten Sie den untenstehenden detaillierten Code wie angefordert.

Datei-Download-Funktion

public void downloadAndSaveFile(String url, String directoryId, String fileName) { 
    HttpURLConnection conn = null; 
    try { 
     Log.d(TAG, "DownloadFileTask url : " + url); 
     conn = getGETConnection(url); 
     conn.setRequestProperty("Accept", "application/json"); 
     conn.setRequestProperty("Content-Type", "application/json"); 
     conn.setRequestProperty("Authorization", "Bearer " + MY_AUTH_TOKEN); 
     conn.connect(); 
     File file = new File(FileTools.getCacheFileLocation(fileName, directoryId)); 
     FileOutputStream fileOutput = new FileOutputStream(file); 
     InputStream inputStream = (InputStream) conn.getInputStream(); 
     byte[] buffer = new byte[1024 * 1024]; 
     int bufferLength = 0; 
     while ((bufferLength = inputStream.read(buffer)) > 0) { 
      fileOutput.write(buffer, 0, bufferLength); 
     } 
     fileOutput.close(); 
     inputStream.close(); 
     Log.d(TAG, "Download successful. Downloaded File : " + file.getAbsolutePath()); 
     // Generate thumbnail and encrypt the file and thumbnail 
     ***String thumbnailPath = FileTools.cacheThumbnail(file, file.getName(), directoryId);*** 
     if (thumbnailPath != null && !thumbnailPath.isEmpty()) { 
      // Encrypt the file 
      try { 
       FileTools.SaveFileEncrypted(file, directoryId); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       // Delete the file 
       file.delete(); 
      } 
     } else { 
      // Delete the file 
      file.delete(); 
     } 
    } catch (Exception e) { 
     Log.d(TAG, "Exception while downloading file"); 
     e.printStackTrace(); 
    } finally { 
     if (conn != null) { 
      conn.disconnect(); 
     } 
    } 
} 

Die FileTools.cacheThumbnail Funktion

public static String cacheThumbnail(File file, String fileName, String chatId) { 
    Log.d(TAG, "cacheThumbnail original File path : " + file.getAbsolutePath()); 
    Log.d(TAG, "cacheThumbnail original File exists : " + file.exists()); 
    Log.d(TAG, "cacheThumbnail original file size : " + file.length()); 
    String thumbName = String.format("thumb-%s.jpg", fileName); 
    File fileThumb = new File(FileTools.getMediaCachePath(chatId), thumbName); 
    ByteArrayOutputStream out = null; 
    try { 
     out = new ByteArrayOutputStream(); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     Bitmap image = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.toURI().getPath(), options), 256, 256, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); 
     // Thumbnail generation for the image failed. It is a video file. Generate 
     // thumbnail for the video file 
     if (image == null) { 
      MediaMetadataRetriever retriever = new MediaMetadataRetriever(); 
      try { 
       retriever.setDataSource(FileTools.getFileInputStreamFromStorage(file).getFD()); 
       // Generate thumbnail from a frame that is 5% deep into the video 
       String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); 
       long timeInMillisec = Long.parseLong(time); 
       long durationMicroSec = timeInMillisec * 1000; 
       long thumbnailDepth = (long) (durationMicroSec * (0.15f)); 
       image = retriever.getFrameAtTime(thumbnailDepth, MediaMetadataRetriever.OPTION_CLOSEST); 
       image = ThumbnailUtils.extractThumbnail(image, 256, 256, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); 
      } catch (IllegalArgumentException ex) { 
       Log.e(TAG, ex.getMessage()); 
       image = null; 
      } catch (RuntimeException ex) { 
       Log.e(TAG, ex.getMessage()); 
       image = null; 
      } catch (IOException e) { 
       image = null; 
       Log.e(TAG, e.getMessage()); 
      } 
     } 
     // Could not generate a thumbnail. Probably a corrupt/bad file 
     if (image == null) { 
      return null; 
     } 
     ***image = Utilities.orientBitmap(file.getAbsolutePath(), image);*** 
     image.compress(Bitmap.CompressFormat.JPEG, 80, out); 
     out.close(); 
     FileTools.SaveFileEncrypted(fileThumb, out.toByteArray()); 
     return fileThumb.toURI().getPath(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } finally { 
     try { 
      if (out != null) { 
       out.close(); 
      } 
     } catch (Exception ignore) { 
     } 
    } 
} 

Die orientBitmap und rotateBitmap Funktionen funktionieren

public static Bitmap orientBitmap(String filePath, Bitmap bitmap) throws IOException { 
    Log.d(TAG, "orientBitmap FilePath : " + filePath); 
    File file = new File(filePath); 
    Log.d(TAG, "orientBitmap File exists : " + file.exists()); 

    ExifInterface exifInterface = new ExifInterface(filePath); 
    Log.d(TAG, "After exception"); 
    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

    Log.d(TAG, "Thumb orientation : " + orientation); 

    switch (orientation) { 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      bitmap = rotateBitmap(bitmap, 90); 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_180: 
      bitmap = rotateBitmap(bitmap, 180); 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_270: 
      bitmap = rotateBitmap(bitmap, 270); 
      break; 
    } 
    return bitmap; 
} 

public static Bitmap rotateBitmap(Bitmap source, float angle) { 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(angle); 
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); 
} 

Ich Aktualisierung der Stack-Traces durch die Veröffentlichung Stack-Trace für wenn versucht wird, das Thumbnail einer mp4-Datei und eines png-Fils zu erzeugen e.

Stack-Trace für MP4-Datei.

D/DownloadFileClass: downloadAndSaveFile url : example.com/media/download/568405 
D/DownloadFileClass: Download successful. Downloaded File : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/MP4_20161017_134641.mp4 
D/FileTools: cacheThumbnail original File path : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/MP4_20161017_134641.mp4 
D/FileTools: cacheThumbnail original File exists : true 
D/FileTools: cacheThumbnail original file size : 6434816 
D/skia: --- SkImageDecoder::Factory returned null 
D/Utilities: orientBitmap FilePath : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/MP4_20161017_134641.mp4 
D/Utilities: orientBitmap File exists : true 
W/ExifInterface: Invalid image. 
    java.io.IOException: Invalid marker: 0 
    at android.media.ExifInterface.getJpegAttributes(ExifInterface.java:1600) 
      at android.media.ExifInterface.loadAttributes(ExifInterface.java:1339) 
      at android.media.ExifInterface.<init>(ExifInterface.java:1057) 
      at com.mypackage.helpers.Utilities.orientBitmap(Utilities.java:85) 
      at com.mypackage.fileio.FileTools.cacheThumbnail(FileTools.java:700) 
      at com.mypackage.coreapi.DownloadFileClass$downloadAndSaveFile(DownloadFileClass.java:215) 
      at com.mypackage.coreapi.DownloadFileClass$downloadAndSaveFile(DownloadFileClass.java:113) 
      at android.os.AsyncTask$2.call(AsyncTask.java:295) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
      at java.lang.Thread.run(Thread.java:818) 
D/Utilities: After exception 
D/Utilities: Thumb orientation : 0 
D/FileTools: Save file to : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/MP4_20161017_134641.mp4 
D/EncryptedFileTools: ......Final saved file path...... : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/encryptPlaceHolder 

Stapel-Trace für Datei png

D/DownloadFileClass: downloadAndSaveFile url : example.com/media/download/568406 
D/DownloadFileClass: Download successful. Downloaded File : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/PNG_20161017_134748.png 
D/FileTools: cacheThumbnail original File path : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/PNG_20161017_134748.png 
D/FileTools: cacheThumbnail original File exists : true 
D/FileTools: cacheThumbnail original file size : 92160 
D/Utilities: orientBitmap FilePath : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/PNG_20161017_134748.png 
D/Utilities: orientBitmap File exists : true 
W/ExifInterface: Invalid image. 
    java.io.IOException: Invalid marker: 89 
      at android.media.ExifInterface.getJpegAttributes(ExifInterface.java:1600) 
      at android.media.ExifInterface.loadAttributes(ExifInterface.java:1339) 
      at android.media.ExifInterface.<init>(ExifInterface.java:1057) 
      at com.mypackage.helpers.Utilities.orientBitmap(Utilities.java:85) 
      at com.mypackage.fileio.FileTools.cacheThumbnail(FileTools.java:700) 
      at com.mypackage.coreapi.DownloadFileClass$downloadAndSaveFile(DownloadFileClass.java:215) 
      at com.mypackage.coreapi.DownloadFileClass$downloadAndSaveFile(DownloadFileClass.java:113) 
      at android.os.AsyncTask$2.call(AsyncTask.java:295) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
      at java.lang.Thread.run(Thread.java:818) 
D/Utilities: After exception 
D/Utilities: Thumb orientation : 0 
D/FileTools: Save file to : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/PNG_20161017_134748.png 
D/EncryptedFileTools: ......Final saved file path...... : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/encryptPlaceHolder 

Das verwirrende Teil ist, dass ich nicht diese Ausnahme bekomme, wenn ich die App auf meinem Samsung Galaxy S4 mit Android 4.4.4 laufen, aber ich es immer bekommen wenn ich es auf meinem Samsung Galaxy S5 mit Android 6.0.1 laufen lasse. Ich bekomme diesen Fehler auch auf dem Emulator mit Android 7.0. Ich bin mir nicht sicher, wie ich dieses Problem angehen soll.

Bitte beachten Sie die beigefügten Berechtigungen Screenshot. Enabled Permissions

Ich habe die angeforderten Informationen hinzugefügt. Bitte lassen Sie mich wissen, wenn weitere Informationen benötigt werden. Ich habe noch nie mit ExifTags gearbeitet, also bitte ertragen Sie mich. Danke nochmal.

+0

Haben Sie Zugriff auf die Datei? Android 6.0 erfordert, dass Sie zur Laufzeit externe Speicherberechtigungen anfordern, wenn Sie API 23 oder höher als Ziel haben. – ianhanniballake

+0

@ianhanniballake Vielen Dank für Ihre Antwort. Ich habe alle Berechtigungen aktiviert. Ich habe den Screenshot der Berechtigungsseite als Referenz hinzugefügt. Bitte lassen Sie mich wissen, wenn Sie weitere Informationen benötigen. – rbing

+1

Welchen Weg führen Sie zur Methode? Stellen Sie auch sicher, dass die Datei existiert – Sanjeet

Antwort

2

Ich habe das Problem mit meinem Code oben gefunden. Anscheinend habe ich versucht, die EXIF-Tags direkt aus mp4- und png-Dateien zu lesen. Bei der Suche nach EXIF-Tags habe ich festgestellt, dass EXIF-Tags nur mit JPEG-Dateien verfügbar sind. Die Fehlerbehebung für mein Problem bestand darin, zunächst ein JPEG-Bild der mp4/png-Datei zu erstellen und dann die EXIF-Tags aus dieser JPEF-Datei zu lesen. Dadurch wurde das Problem behoben und ich erhalte die Ausnahme nicht mehr.

Danke an alle, die mich zur Lösung geführt haben.

+0

Wie Sie das Problem behoben haben, können Sie pls erklären? – Praneeth

+0

@praneethkumar Wie ich oben erwähnt habe, habe ich versucht, EXIF-Tags direkt aus mp4/png-Dateien zu lesen. Da EXIF-Tags nur mit JPEG-Dateien verknüpft sind, wurde dieser Fehler angezeigt, als ich versuchte, sie aus der mp4/png-Datei zu lesen. Ich verhindere den Fehler, indem ich zuerst ein JPEG aus der mp4/png-Datei erstelle und dann versuche, die EXIF-Tags aus der JPEG-Datei zu lesen. Dieser Ansatz verhindert den Fehler, aber ich glaube nicht, dass Sie EXIF-Informationen von der JPEG-Datei erhalten, die von MP4/PNG erstellt wurde, es sei denn, Sie explizit EXIF-Werte für die JPEG-Datei festlegen, nachdem Sie eine aus MP4/PNG-Datei erstellen. – rbing