Parcourir la source

1.修改Android Apex Drivers图像下载保存后数据有丢失以及照片显示方向不正确。

Pen Li il y a 7 ans
Parent
commit
6719398aab

+ 47 - 0
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/utils/FileManager.java

@@ -10,6 +10,7 @@ import android.support.v4.content.FileProvider;
 import android.util.Log;
 import android.webkit.MimeTypeMap;
 
+import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
@@ -113,6 +114,52 @@ public class FileManager {
         return file;
     }
 
+    public static void saveBytes2Path(byte[] bytes,String filename) {
+        // TODO Auto-generated method stub
+
+
+        File file = new File(filename);
+        // 创建FileOutputStream对象
+        FileOutputStream outputStream = null;
+        // 创建BufferedOutputStream对象
+        BufferedOutputStream bufferedOutputStream = null;
+        try {
+            // 如果文件存在则删除
+            if (file.exists()) {
+                file.delete();
+            }
+            // 在文件系统中根据路径创建一个新的空文件
+            file.createNewFile();
+            // 获取FileOutputStream对象
+            outputStream = new FileOutputStream(file);
+            // 获取BufferedOutputStream对象
+            bufferedOutputStream = new BufferedOutputStream(outputStream);
+            // 往文件所在的缓冲输出流中写byte数据
+            bufferedOutputStream.write(bytes);
+            // 刷出缓冲输出流,该步很关键,要是不执行flush()方法,那么文件的内容是空的。
+            bufferedOutputStream.flush();
+        } catch (Exception e) {
+            // 打印异常信息
+            e.printStackTrace();
+        } finally {
+            // 关闭创建的流对象
+            if (outputStream != null) {
+                try {
+                    outputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (bufferedOutputStream != null) {
+                try {
+                    bufferedOutputStream.close();
+                } catch (Exception e2) {
+                    e2.printStackTrace();
+                }
+            }
+        }
+    }
+
     public static String getMimeType(String filePath) {
         String ext = MimeTypeMap.getFileExtensionFromUrl(filePath);
         String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

+ 63 - 11
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/utils/ImageUtil.java

@@ -15,10 +15,14 @@ import android.support.v4.graphics.drawable.DrawableCompat;
 import android.util.Log;
 
 import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
+import java.net.URL;
 
 public class ImageUtil {
 
@@ -114,6 +118,23 @@ public class ImageUtil {
         return resizedBitmap;
     }
 
+    public static Bitmap adjustPhotoOrientation(String filename) {
+        if (filename == null) {
+            return null;
+        }
+
+        int degree = readPictureDegree(filename);
+
+        Bitmap bitmap = BitmapFactory.decodeFile(filename);
+
+        if (degree != 0) {
+
+            return rotaingImageView(degree,bitmap);
+        }
+
+        return bitmap;
+    }
+
     /**
      * @param context ApplicationContext
      * */
@@ -132,6 +153,23 @@ public class ImageUtil {
                 });
     }
 
+    public static String imageCachePath(Context context, URI uri) {
+
+        if (context == null || uri == null) {
+            return null;
+        }
+
+        String scheme = uri.getScheme();
+        if (scheme == null) {
+            return uri.toString();
+        }
+
+        String md5 = RAUtil.stringToMD5(uri.toString());
+        String imgDir = imageCacheDir(context);
+
+        return imgDir + File.separator + md5;
+    }
+
     public static Bitmap loadImageFromURL(Context context, URI uri) {
         if (uri == null) {
             return null;
@@ -147,7 +185,11 @@ public class ImageUtil {
 
         Bitmap bitmap = null;
         if (context == null) {
-            bitmap = Network.getImageFromLink(uri.toString());
+//            bitmap = Network.getImageFromLink(uri.toString());
+
+            byte[] bytes = Network.getByteFromURL(uri.toString());
+            bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
+
             return bitmap;
         } else {
 
@@ -175,19 +217,29 @@ public class ImageUtil {
 
             if (bitmap == null) {
 
-                bitmap = Network.getImageFromLink(uri.toString());
-                if (bitmap != null) {
-                    try {
-                        if (!imgFile.exists()) {
-                            imgFile.createNewFile();
-                        }
-                        savePhotoToFile(bitmap,imgFile);
+                byte[] bytes = Network.getByteFromURL(uri.toString());
+                bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
 
-                    } catch (IOException e) {
-                        e.printStackTrace();
-                    }
+                if (bytes != null && bytes.length > 0) {
+                    FileManager.saveBytes2Path(bytes,imgFile.toString());
                 }
 
+                // 以下方式下载保存的文件数据有丢失
+
+//            bitmap = Network.getImageFromLink(uri.toString());
+
+//                if (bitmap != null) {
+//                    try {
+//                        if (!imgFile.exists()) {
+//                            imgFile.createNewFile();
+//                        }
+//                        saveJPGToFile(bitmap,imgFile);
+//
+//                    } catch (IOException e) {
+//                        e.printStackTrace();
+//                    }
+//                }
+
             }
 
             return bitmap;

+ 34 - 0
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/utils/Network.java

@@ -15,6 +15,7 @@ import org.json.JSONObject;
 
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -442,6 +443,39 @@ public class Network {
         return null;
     }
 
+    public static byte[] getByteFromURL(String link) {
+
+        HttpURLConnection connection = null;
+        try {
+
+            URL url = new URL(link);
+            connection = (HttpURLConnection)url.openConnection();
+            connection.setRequestMethod("GET");
+            InputStream inputStream = connection.getInputStream();
+
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            byte[] buf = new byte[100];
+            int len = 0;
+            while ((len = inputStream.read(buf)) != -1) {
+                baos.write(buf,0,len);
+            }
+            byte[] bytes = baos.toByteArray();
+            inputStream.close();
+            baos.close();
+
+            return bytes;
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+        return null;
+
+    }
+
     public static Bitmap getImageFromLink(String link) {
         HttpURLConnection connection = null;
         Bitmap img = null;

+ 8 - 1
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/detail/model/DetailPhotoModel.java

@@ -59,7 +59,14 @@ public class DetailPhotoModel extends DetailBaseModel {
 
                 try {
 
-                    return ImageUtil.loadImageFromURL(mCtx,new URI(photoURL));
+                    URI uri = new URI(photoURL);
+
+                    ImageUtil.loadImageFromURL(mCtx,uri);
+
+                    String filename = ImageUtil.imageCachePath(mCtx,uri);
+                    Bitmap bitmap = ImageUtil.adjustPhotoOrientation(filename);
+
+                    return bitmap;
 
                 } catch (URISyntaxException e) {
                     e.printStackTrace();