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

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

可配合前台线程显示

 

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

你可能感兴趣的:(Android开发)