Android 将图片网址url转化为bitmap,drawable转bitmap,file转bitmap,bitmap转file

Android开发 专栏收录该内容
17 篇文章 0 订阅

file转bitmap

File param = new File();

 

Bitmap bitmap= BitmapFactory.decodeFile(param.getPath());

drawable转bitmap

 

Bitmap    bmp = BitmapFactory.decodeResource(getResources(),R.mipmap.jcss_03 );

url转bitmap

 

Bitmap bitmap;
public Bitmap returnBitMap(final String url){

    new Thread(new Runnable() {
        @Override
        public void run() {
            URL imageurl = null;

            try {
                imageurl = new URL(url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            try {
                HttpURLConnection conn = (HttpURLConnection)imageurl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();
                bitmap = BitmapFactory.decodeStream(is);
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

    return bitmap;
}

 

方法二:

public Bitmap getBitmap(String url) {
    Bitmap bm = null;
    try {
        URL iconUrl = new URL(url);
        URLConnection conn = iconUrl.openConnection();
        HttpURLConnection http = (HttpURLConnection) conn;

        int length = http.getContentLength();

        conn.connect();
        // 获得图像的字符流
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is, length);
        bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();// 关闭流
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return bm;
}

 

可配合前台线程显示

 

private Handler mHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        switch (msg.what) {
            case REFRESH_COMPLETE:
                myheadimage.setImageBitmap(bitmap);//显示
                break;
        }
    }
};
String imageUrl = "http://www.pp3.cn/uploads/201511/2015111212.jpg";
bitmap= returnBitMap(imageUrl);
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 1000);

bitmap转file

private  String SAVE_PIC_PATH = Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)
        ? Environment.getExternalStorageDirectory().getAbsolutePath() : "/mnt/sdcard";//

private  String SAVE_REAL_PATH = SAVE_PIC_PATH + "/good/savePic";//保存的确

 

saveFile(bmp, System.currentTimeMillis() + ".png");
  //保存方法
    private void saveFile(Bitmap bm, String fileName) throws IOException {
        String subForder = SAVE_REAL_PATH;
        File foder = new File(subForder);
        if (!foder.exists()) foder.mkdirs();

        File myCaptureFile = new File(subForder, fileName);
        Log.e("lgq","图片保持。。。。wwww。。。。"+myCaptureFile);
        ends = myCaptureFile.getPath();
        if (!myCaptureFile.exists()) myCaptureFile.createNewFile();
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        bos.close();
//        ToastUtil.showSuccess(getApplicationContext(), "已保存在/good/savePic目录下", Toast.LENGTH_SHORT);
        //发送广播通知系统
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(myCaptureFile);
        intent.setData(uri);
        this.sendBroadcast(intent);
    }

bitmap与byte[]之间相互转换

 

Android 图片压缩,bitmap与byte[]之间相互转换:https://blog.csdn.net/meixi_android/article/details/89921090

  • 5
    点赞
  • 4
    评论
  • 14
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值