package com.thfund.bigtree.StarCounselor.activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.provider.MediaStore; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.thfund.bigtree.R; import com.thfund.bigtree.base.BaseActivity; import com.thfund.bigtree.utils.ToastUtil; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class StarWeChatQRActivity extends BaseActivity implements View.OnClickListener { private ImageView showImageView;//显示图片ImageView private Button mSaveBtn;//保存图片按钮 private String imgURl;//图片的URL地址 private static final int SAVE_SUCCESS = 0;//保存图片成功 private static final int SAVE_FAILURE = 1;//保存图片失败 private static final int SAVE_BEGIN = 2;//开始保存图片 private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SAVE_BEGIN: ToastUtil.showMsg("开始保存图片..."); mSaveBtn.setClickable(false); break; case SAVE_SUCCESS: ToastUtil.showMsg("图片保存成功,请到相册查找"); mSaveBtn.setClickable(true); break; case SAVE_FAILURE: ToastUtil.showMsg("图片保存失败,请稍后再试..."); mSaveBtn.setClickable(true); break; } } }; /** * 打开该Activity的一种静态方式 * * @param context * @param imgURl */ public static void startActivity(Context context, String imgURl) { Intent intent = new Intent(context, StarWeChatQRActivity.class); Bundle bundle = new Bundle(); bundle.putString("imgURl", imgURl); intent.putExtras(bundle); context.startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_star_we_chat_qr); initViews(); initData(); } /** * 初始化控件 */ private void initViews() { showImageView = (ImageView) findViewById(R.id.image_view); mSaveBtn = (Button) findViewById(R.id.save_btn); mSaveBtn.setOnClickListener(this); } /** * 初始化数据 */ private void initData() { //我的ImageURL是上一级传参过来的 imgURl = getIntent().getStringExtra("imgURl"); //用Glide将图片加载出来 Glide.with(mContext).load(imgURl).fitCenter().crossFade().into(showImageView); } /** * 点击事件处理 * * @param v */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.save_btn://保存图片 mSaveBtn.setClickable(false);//不可重复点击 //保存图片必须在子线程中操作,是耗时操作 new Thread(new Runnable() { @Override public void run() { mHandler.obtainMessage(SAVE_BEGIN).sendToTarget(); Bitmap bp = returnBitMap(imgURl); saveImageToPhotos(mContext, bp); } }).start(); break; } } /** * 保存二维码到本地相册 */ private void saveImageToPhotos(Context context, Bitmap bmp) { // 首先保存图片 File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee"); if (!appDir.exists()) { appDir.mkdir(); } String fileName = System.currentTimeMillis() + ".jpg"; File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 其次把文件插入到系统图库 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null); } catch (FileNotFoundException e) { e.printStackTrace(); mHandler.obtainMessage(SAVE_FAILURE).sendToTarget(); return; } // 最后通知图库更新 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(file); intent.setData(uri); context.sendBroadcast(intent); mHandler.obtainMessage(SAVE_SUCCESS).sendToTarget(); } /** * 将URL转化成bitmap形式 * * @param url * @return bitmap type */ public final static Bitmap returnBitMap(String url) { URL myFileUrl; Bitmap bitmap = null; try { myFileUrl = new URL(url); HttpURLConnection conn; conn = (HttpURLConnection) myFileUrl.openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } /** * 点击除了按钮的任何一个地方,finish掉 * * @param event * @return */ @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: finish(); break; } return super.onTouchEvent(event); } }
Android下载网络图片并保存在本地相册
最新推荐文章于 2024-08-08 15:37:45 发布