Android 将图片网址url转化为bitmap,drawable转bitmap,file转bitmap,bitmap转file
生活随笔
收集整理的這篇文章主要介紹了
Android 将图片网址url转化为bitmap,drawable转bitmap,file转bitmap,bitmap转file
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
file轉(zhuǎn)bitmap
File param = new File();
?
Bitmap bitmap= BitmapFactory.decodeFile(param.getPath());drawable轉(zhuǎn)bitmap
?
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.mipmap.jcss_03 );url轉(zhuǎn)bitmap
?
Bitmap bitmap; public Bitmap returnBitMap(final String url){new Thread(new Runnable() {@Overridepublic 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();// 關(guān)閉流}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轉(zhuǎn)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);//發(fā)送廣播通知系統(tǒng)Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);Uri uri = Uri.fromFile(myCaptureFile);intent.setData(uri);this.sendBroadcast(intent);}bitmap與byte[]之間相互轉(zhuǎn)換
?
Android 圖片壓縮,bitmap與byte[]之間相互轉(zhuǎn)換:https://blog.csdn.net/meixi_android/article/details/89921090
總結(jié)
以上是生活随笔為你收集整理的Android 将图片网址url转化为bitmap,drawable转bitmap,file转bitmap,bitmap转file的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery中的height()、inn
- 下一篇: 计算机双面打印设置,双面打印怎么设置?双