3adb8257bff01e9d9bb97cd04c08d652110b6a92.svn-base 5.6 KB

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