日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2023/12/10 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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() {@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();// 關閉流}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

總結

以上是生活随笔為你收集整理的Android 将图片网址url转化为bitmap,drawable转bitmap,file转bitmap,bitmap转file的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。