服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java异步上传图片示例

java异步上传图片示例

2019-11-06 11:20java教程网 JAVA教程

这篇文章主要介绍了java异步上传图片示例,需要的朋友可以参考下

代码如下:


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

延伸 · 阅读

精彩推荐