亲宝软件园·资讯

展开

java异步传图片 java异步上传图片示例

人气:0
想了解java异步上传图片示例的相关内容吗,在本文为您仔细讲解java异步传图片的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:异步上传,图片,下面大家一起来学习吧。

复制代码 代码如下:

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

加载全部内容

相关教程
猜你喜欢
用户评论