0b054ed482dfb15497256bf5a154f6c3f59533cc.svn-base 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package eVVM.apk.helper;
  2. import android.app.ProgressDialog;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.net.Uri;
  8. import android.os.Environment;
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.text.TextUtils;
  12. import android.util.Log;
  13. import android.widget.Toast;
  14. import java.io.BufferedOutputStream;
  15. import java.io.File;
  16. import java.io.FileNotFoundException;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.net.HttpURLConnection;
  21. import java.net.URL;
  22. import java.util.UUID;
  23. /**
  24. * Created by vimi8 on 2017/6/9.
  25. */
  26. public class ImgDonwloads {
  27. private static String filePath;
  28. private static Bitmap mBitmap;
  29. private static String mFileName = "sshs";
  30. private static String mSaveMessage;
  31. private final static String TAG = "ImageActivity";
  32. private static Context context;
  33. private static ProgressDialog mSaveDialog = null;
  34. public static void donwloadImg(Context contexts, String filePaths) {
  35. context = contexts;
  36. filePath = filePaths;
  37. mSaveDialog = ProgressDialog.show(context, "保存图片", "图片正在保存中,请稍等...", true);
  38. new Thread(saveFileRunnable).start();
  39. }
  40. private static Runnable saveFileRunnable = new Runnable() {
  41. @Override
  42. public void run() {
  43. try {
  44. // mBitmap = BitmapFactory.decodeStream(getImageStream(filePath));
  45. if (!TextUtils.isEmpty(filePath)) { //网络图片
  46. //对资源链接
  47. URL url = new URL(filePath);
  48. //打开输入流
  49. InputStream inputStream = url.openStream();
  50. //对网上资源进行下载转换位图图片
  51. mBitmap = BitmapFactory.decodeStream(inputStream);
  52. inputStream.close();
  53. }
  54. // Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),R.drawable.bg_guid_1 ); //本地的图片,根据自己情况
  55. saveFile(mBitmap);
  56. // saveFile(bmp);
  57. mSaveMessage = "图片已保存到相册";
  58. } catch (IOException e) {
  59. mSaveMessage = "图片保存失败!";
  60. e.printStackTrace();
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. messageHandler.sendMessage(messageHandler.obtainMessage());
  65. }
  66. };
  67. private static Handler messageHandler = new Handler() {
  68. @Override
  69. public void handleMessage(Message msg) {
  70. mSaveDialog.dismiss();
  71. Log.d(TAG, mSaveMessage);
  72. Toast.makeText(context, mSaveMessage, Toast.LENGTH_SHORT).show();
  73. }
  74. };
  75. /**
  76. * Get image from newwork
  77. *
  78. * @param path The path of image
  79. * @return InputStream
  80. * @throws Exception
  81. */
  82. public static InputStream getImageStream(String path) throws Exception {
  83. URL url = new URL(path);
  84. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  85. conn.setConnectTimeout(5 * 1000);
  86. conn.setRequestMethod("GET");
  87. if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
  88. return conn.getInputStream();
  89. }
  90. return null;
  91. }
  92. /**
  93. * 保存文件
  94. *
  95. * @param bm
  96. * @param fileName
  97. * @throws IOException
  98. */
  99. public static void saveFile(Bitmap bm, String fileName) throws IOException {
  100. File dirFile = new File(Environment.getExternalStorageDirectory().getPath());
  101. if (!dirFile.exists()) {
  102. dirFile.mkdir();
  103. }
  104. fileName = UUID.randomUUID().toString() + ".jpg";
  105. File myCaptureFile = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + fileName);
  106. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
  107. bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
  108. bos.flush();
  109. bos.close();
  110. //把图片保存后声明这个广播事件通知系统相册有新图片到来
  111. Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  112. Uri uri = Uri.fromFile(myCaptureFile);
  113. intent.setData(uri);
  114. context.sendBroadcast(intent);
  115. }
  116. /**
  117. * 保存图片到图库
  118. *
  119. * @param bmp
  120. */
  121. public static void saveFile(Bitmap bmp) {
  122. // 首先保存图片
  123. File appDir = new File(Environment.getExternalStorageDirectory(),
  124. "sshs");
  125. if (!appDir.exists()) {
  126. appDir.mkdir();
  127. }
  128. String fileName = System.currentTimeMillis() + "authImage" + ".jpg";
  129. File file = new File(appDir, fileName);
  130. try {
  131. FileOutputStream fos = new FileOutputStream(file);
  132. bmp.compress(Bitmap.CompressFormat.JPEG, 80, fos);
  133. fos.flush();
  134. fos.close();
  135. } catch (FileNotFoundException e) {
  136. e.printStackTrace();
  137. } catch (IOException e) {
  138. e.printStackTrace();
  139. }
  140. // 其次把文件插入到系统图库
  141. /* try {*/
  142. /* MediaStore.Images.Media.insertImage(context.getContentResolver(),
  143. bmp, fileName, null);
  144. file.delete();*/
  145. /* } catch (FileNotFoundException e) {
  146. e.printStackTrace();
  147. }*/
  148. // 最后通知图库更新
  149. context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
  150. Uri.fromFile(new File(file.getPath()))));
  151. }
  152. }