首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileOutputStream错误

FileOutputStream错误
EN

Stack Overflow用户
提问于 2010-04-27 08:13:45
回答 1查看 2.5K关注 0票数 1

我有一部分代码,这些代码应该从网站获取一个图像并将其存储到sdcard中。当我在sdk1.5上开发时,下面的代码正在使用find。但是,在我将其更改为AndroidSDK2.0之后,它现在就无法工作了。这一行出了问题: FileOutputStream fos =新FileOutputStream(filepath + "/“+ this.filename);

下面是我的代码:

代码语言:javascript
复制
  void downloadFile(String fileUrl) {
    URL myFileUrl = null;
    try {
        myFileUrl = new URL(fileUrl);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();

        InputStream is = conn.getInputStream();

        bmImg = BitmapFactory.decodeStream(is);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        String filepath = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        FileOutputStream fos = new FileOutputStream(filepath + "/"
                + this.filename);
        bmImg.compress(CompressFormat.JPEG, 75, fos);
        fos.flush();
        fos.close();

        Context context = this.getBaseContext();
        new MediaScannerNotifier2(context, filepath + "/" + this.filename,
                "image/jpeg");

        // displaying download completion message
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Wallpaper Downloaded").setCancelable(false)
                .setPositiveButton("ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                                btn_setwall.setEnabled(true);
                                btn_download.setEnabled(false);
                            }
                        });
        AlertDialog alert = builder.create();
        alert.show();
    } catch (Exception e) {
        Log.e("MyLog", e.toString());
    }

}

错误发生在第三次捕获。但是,当我移动这条线时

FileOutputStream fos =新FileOutputStream(filepath + "/“+ this.filename);

对于第二次尝试/捕捉,那么它将发生在第二次捕获。能帮我一下吗?

EN

回答 1

Stack Overflow用户

发布于 2010-10-07 18:11:12

也许试着摆脱.getAbsolutePath()

这对我来说是适用于2.2的:

代码语言:javascript
复制
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + fileName);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2719661

复制
相关文章

相似问题

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