English 中文(简体)
使用外部压力 公共日报 机器人档案被损坏
原标题:Android files get damaged using getExternalStoragePublicDirectory
  • 时间:2012-05-23 08:53:21
  •  标签:
  • android

当我想从我的应用程序中把图像保存到Android时,我遇到了一个奇怪的问题。这些图像现在被损坏了, Android cant 使用这些图像。 我看不出我的代码有什么问题。

以下是我的下载方法:

    public void createExternalStoragePublicPicture(String DownloadUrl, String fileName) {
    // Create a path where we will place our picture in the user s
    // public pictures directory.  Note that you should be careful about
    // what you place here, since the user often manages these files.  For
    // pictures and other media owned by the application, consider
    // Context.getExternalMediaDir().

    Log.i("Download", DownloadUrl);
    Log.i("File", fileName);

    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File file = new File(path, fileName);

    try {
        // Make sure the Pictures directory exists.
        path.mkdirs();

        // Very simple code to copy a picture from the application s
        // resource into the external file.  Note that this code does
        // no error checking, and assumes the picture is small (does not
        // try to copy it in chunks).  Note that if external storage is
        // not currently mounted this will silently fail.
        URL url = new URL(DownloadUrl);
        URLConnection ucon = url.openConnection();

        InputStream is = ucon.getInputStream();
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();

        // Tell the media scanner about the new file so that it is
        // immediately available to the user.
        MediaScannerConnection.scanFile(this,
                new String[] { file.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                Log.i("ExternalStorage", "Scanned " + path + ":");
                Log.i("ExternalStorage", "-> uri=" + uri);
            }
        });
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}

有什么想法吗? 这里出了什么问题?

最佳回答

可能的话, is. operate () 不像你预期的那样有效。 一般来说, 您不应该依赖 unken () , 因为它没有严格指定返回的值 。

"http:// developmenter.android.com/reference/java/io/InputStream.html#unket%28%29" rel=“nofollow”>InputStream :“特别重要的是,要认识到你决不能使用这种方法来尺寸容器,并假设你能够阅读整个溪流[......]”

使用常见的“ 循环” 近似 :

int c;
while ( (c = is.read(buf)) >= 0 ) {
  os.write(buf,0,c)
}

这可以确保您真的读取源文件中的字节的 all

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签