莫邪女郎 2014-05-07 08:22 采纳率: 0%
浏览 4376

android下载图片保存到本地图片大小为0KB,是什么原因??求大神解决

public Drawable loadImageFromUrl(String urlPath, String directory,
String filename) {
byte[] data = null;
InputStream is = null;
Drawable drawable = null;
HttpURLConnection conn = null;
URL url = null;
try {
url = new URL(urlPath);
} catch (Exception e) {
e.printStackTrace();
return drawable;
}
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
// conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setConnectTimeout(6000);
is = conn.getInputStream();
if (conn.getResponseCode() == 200) {
BitmapFactory.Options opts = new BitmapFactory.Options();
data = readInputStream(is);
//处理图片溢出
opts.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, opts);
opts.inSampleSize = computeSampleSize(opts, -1, AppContext.getScreenHeight()*AppContext.getScreenWidth());
opts.inJustDecodeBounds = false;

            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
                    data.length, opts);

            drawable = (Drawable) new BitmapDrawable(bitmap);
            // 如果目录和图片名称存在,保存图片
            if (null != directory && null != filename) {
                // 创建目录,保存图片
                if (hasStorage(true, directory)) {
                    // 目录地址
                    String directoryName = SDPATH + directory;
                    // 创建文件
                    File file = new File(directoryName, filename);

                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(file));

                    // 获取图片的格式
                    String dat = filename.substring(filename
                            .lastIndexOf(".") + 1);
                    Log.d(TAG, "图片格式1111111111:" + dat);
                    if ("jpg".equals(dat)) {
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bos);
                    } else if ("png".equals(dat)) {
                        bitmap.compress(Bitmap.CompressFormat.PNG, 90, bos);
                    }

                    bos.flush();
                    bos.close();
                } else {
                    Log.d(TAG, "save picture failed");
                }
            }

        } else {
            data = null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        drawable = null;
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        conn.disconnect();
    }

    return drawable;
}
  • 写回答

1条回答

  • icyblood198999 2014-05-09 06:03
    关注

    0k的话,主要还是考虑请求没有成功,图片根本没有传到android端的情况

    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型