在尝试使用文件路径初始化exif接口实例时,我无法找出ExifInterface构造函数抛出的异常.
更新
请根据要求查看下面的详细代码.
文件下载功能
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(); } }}
fileTools.cachethumbnail函数
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) { } }}
orIEntBitmap和rotateBitmap函数功能
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);}
我在尝试生成mp4文件和png文件的缩略图时通过发布堆栈跟踪来更新堆栈跟踪.
mp4文件的堆栈跟踪.
D/DownloadfileClass: downloadAndSavefile url : example.com/media/download/568405D/DownloadfileClass: Download successful. Downloaded file : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/MP4_20161017_134641.mp4D/fileTools: cachethumbnail original file path : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/MP4_20161017_134641.mp4D/fileTools: cachethumbnail original file exists : trueD/fileTools: cachethumbnail original file size : 6434816D/skia: --- Skimagedecoder::Factory returned nullD/UtilitIEs: orIEntBitmap filePath : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/MP4_20161017_134641.mp4D/UtilitIEs: orIEntBitmap file exists : trueW/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.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at androID.os.AsyncTask$SerialExecutor.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 exceptionD/UtilitIEs: Thumb orIEntation : 0D/fileTools: Save file to : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/MP4_20161017_134641.mp4D/EncryptedfileTools: ......Final saved file path...... : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/encryptPlaceHolder
png文件的堆栈跟踪
D/DownloadfileClass: downloadAndSavefile url : example.com/media/download/568406D/DownloadfileClass: Download successful. Downloaded file : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/PNG_20161017_134748.pngD/fileTools: cachethumbnail original file path : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/PNG_20161017_134748.pngD/fileTools: cachethumbnail original file exists : trueD/fileTools: cachethumbnail original file size : 92160D/UtilitIEs: orIEntBitmap filePath : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/PNG_20161017_134748.pngD/UtilitIEs: orIEntBitmap file exists : trueW/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.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at androID.os.AsyncTask$SerialExecutor.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 exceptionD/UtilitIEs: Thumb orIEntation : 0D/fileTools: Save file to : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/PNG_20161017_134748.pngD/EncryptedfileTools: ......Final saved file path...... : /data/user/0/com.mypackage.myapp/cache/userhash/79640/media/encryptPlaceHolder
令人困惑的部分是,当我使用AndroID 4.4.4在我的Samsung galaxy S4上运行应用程序时,我没有得到这个例外,但是当我在带有androID 6.0.1的Samsung galaxy S5上运行它时,我总是得到它.我也在使用AndroID 7.0的模拟器上出现此错误.我不知道如何解决这个问题.
请参阅附加的权限屏幕截图.
Enabled Permissions
我已添加了所需的信息.如果需要更多信息,请告诉我.我之前没有使用过ExifTags所以请耐心等待.
再次感谢.
解决方法:
我发现上面的代码存在问题.显然我试图直接从mp4和png文件中读取EXIF标签.在搜索有关EXIF标签的更多信息后,我发现EXIF标签仅适用于JPEG文件.因此我的问题的解决方法是首先创建mp4 / png文件的JPEG图像,然后尝试从此JPEF文件中读取EXIF标记.这样做解决了问题,我不再得到例外.
感谢大家指导我解决方案.
总结以上是内存溢出为你收集整理的android – ExifInterface构造函数抛出IOExxception全部内容,希望文章能够帮你解决android – ExifInterface构造函数抛出IOExxception所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)