首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图像下载失败

图像下载失败
EN

Stack Overflow用户
提问于 2014-07-07 06:12:56
回答 1查看 2K关注 0票数 0

如果我下载图片失败了,我怎么知道呢?

我首先下载图像url,然后从那里获取图像文件名以存储到我的数据库中。然后有两个方法从url下载,并保存为文件名。顺便说一句,它们是由一个AsyncTask调用的方法调用的。

以下是处理图像文件下载的两种方法(由我的大四学生代代相传):

代码语言:javascript
复制
private void imageProcessing(String url, String filename) {

    String root = Environment.getExternalStorageDirectory().toString();
    // String root1= getResources().getIdentifier(name, defType, defPackage)
    File myDir = new File(root + "/arson/images");
    File nomedia = new File(myDir, ".nomedia");
    if (!nomedia.exists()) {
        Log.wtf("nomedia not exists", nomedia.getAbsolutePath().toString());
        try {
            nomedia.createNewFile();
        } catch (IOException e1) {
            Log.e("NEW FILE CREATION", e1.toString());
            e1.printStackTrace();
        }
    } else {
        Log.wtf("nomedia exists", nomedia.getAbsolutePath().toString());
    }
    File file = new File(myDir, filename);
    if (file.exists()) {
        file.delete();
    }
    try {
        Bitmap bitmap = downloadBitmap(url);
        myDir.mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        Log.e("BITMAP PROCESS", e.toString());
    }
}

public static Bitmap downloadBitmap(String url) {
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);
    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            return null;
        }
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                System.setProperty("http.keepAlive", "false");
                inputStream = entity.getContent();
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        // Could provide a more explicit error message for IOException or
        // IllegalStateException
        getRequest.abort();
    } finally {
        if (client != null)
            client.close();
    }
    return null;
}

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-07 07:24:55

这里有两种可能的问题。

1) HTTP状态代码不是200 :如果发生这种情况,您将返回null,当您获得null时,您可以知道下载出现了问题。

2) HTTP状态代码为200,但文件下载失败:这里要么有异常,要么根本没有异常。如果你有异常,你已经在捕捉它了。对于另一种情况,在没有例外情况下,您必须稍微更改您的实现。您必须首先保存下载的文件(临时文件),读取contentLength并验证它是否与您从服务器获得的文件相匹配。如果contentLength是正确的,则可以使用BitmapFactory从设备读取文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24604033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档