url下载下来的图片,base64格式上传(base64)

  private void uploadImage() {
        if (NetworkCheck.isNetworkAvailable(getActivity())) {
            UploadThread uploadThread = new UploadThread();
            uploadThread.start();
        } else {
            showToast(getString(R.string.network_has_disconnected));
        }
    }
 class UploadThread extends Thread {
        public void run() {
            String base64 = FileUtil.bitmapToBase64(getBitmap(url));
            Map<String, Object> map = new HashMap<>();
            map.put("image", base64);
            uploadImage(gson.toJson(map), UrlConfig.UPDATE_URL);
        }
    }

   public static 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;
    }


展开阅读全文

没有更多推荐了,返回首页