java异步上传图片示例

时间:2023-05-31 13:42:51 


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();

标签:java,异步上传,图片
0
投稿

猜你喜欢

  • Java实现按权重随机数

    2023-11-28 23:15:32
  • Java 自定义注解的魅力

    2023-06-29 14:45:15
  • Java处理InterruptedException异常的理论与实践

    2023-07-25 03:22:33
  • Android TextView字幕效果实例

    2022-03-18 14:55:13
  • c++异常处理机制示例及详细讲解

    2022-04-27 11:31:27
  • SSH原理及两种登录方法图文详解

    2023-11-14 11:10:53
  • HDFS Balancer负载均衡器及语法详解

    2023-11-18 18:30:39
  • C#中遍历Hashtable的4种方法

    2023-01-18 12:17:51
  • Java全面分析面向对象之继承

    2023-11-23 11:55:59
  • 关于@Scheduled不执行的原因分析

    2021-06-27 17:13:20
  • 详解android 用webview加载网页(https和http)

    2021-12-29 11:14:30
  • slf4j jcl jul log4j1 log4j2 logback各组件系统日志切换

    2023-08-08 13:00:41
  • springboot接收别人上传的本地视频实例代码

    2023-03-22 01:55:39
  • Java Idea TranslationPlugin翻译插件使用解析

    2023-12-01 10:25:02
  • C++多重继承与虚继承分析

    2023-04-22 14:49:16
  • Spring Security 实现用户名密码登录流程源码详解

    2023-05-31 11:13:59
  • SpringCloud使用Zookeeper作为配置中心的示例

    2023-08-03 12:30:51
  • android实现okHttp的get和post请求的简单封装与使用

    2023-10-06 04:20:25
  • Android 破解视频App去除广告功能详解及解决办法总结

    2022-07-16 23:28:50
  • JavaWeb项目部署到服务器详细步骤详解

    2023-11-29 11:15:20
  • asp之家 软件编程 m.aspxhome.com