|
|
@@ -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;
|