源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java异步上传图片示例

  • 时间:2021-12-05 01:34 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java异步上传图片示例
[u]复制代码[/u] 代码如下:
final File imageFile = new File(getCacheDir().getPath() + "/img/" + p.image); image.setVisibility(View.GONE); view.findViewById(R.id.imageLoading).setVisibility(View.VISIBLE); (new AsyncTask<Void, Void, Bitmap>() {     @Override     protected Bitmap doInBackground(Void... params) {         try {             Bitmap image;             if (!imageFile.exists() || imageFile.length() == 0) {                 image = BitmapFactory.decodeStream(new URL(                         "http://example.com/images/"                                 + p.image).openStream());                 image.compress(Bitmap.CompressFormat.JPEG, 85,                         new FileOutputStream(imageFile));                 image.recycle();             }             image = BitmapFactory.decodeFile(imageFile.getPath(),                 bitmapOptions);             return image;         } catch (MalformedURLException ex) {             // TODO Auto-generated catch block             ex.printStackTrace();             return null;         } catch (IOException ex) {             // TODO Auto-generated catch block             ex.printStackTrace();             return null;         }     }     @Override     protected void onPostExecute(Bitmap image) {         if (view.getTag() != p) // The view was recycled.             return;             view.findViewById(R.id.imageLoading).setVisibility(                 View.GONE);         view.findViewById(R.id.image)                 .setVisibility(View.VISIBLE);         ((ImageView) view.findViewById(R.id.image))                 .setImageBitmap(image);     } }).execute();
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部