1

我正在使用带有 metaio 的增强现实开发一个 android 应用程序。好吧,在我的应用程序中,我必须从 url 下载一些图像。但是当我这样做时,我遇到了一个错误,它只获取了文件的一部分。

我有这个:

 04-30 14:43:25.625: W/ExternalStorage(14181): Error writing /mnt/sdcard/Android/data/com.augmentedwine/files/Download/LOL.jpg
 04-30 14:43:25.625: W/ExternalStorage(14181): java.io.IOException: stream closed
 04-30 14:43:25.625: W/ExternalStorage(14181):  at libcore.net.http.AbstractHttpInputStream.checkNotClosed(AbstractHttpInputStream.java:68)
 04-30 14:43:25.625: W/ExternalStorage(14181):  at libcore.net.http.FixedLengthInputStream.available(FixedLengthInputStream.java:59)
 04-30 14:43:25.625: W/ExternalStorage(14181):  at com.augmentedwine.view.ARActivity.createExternalStoragePrivatePicture(ARActivity.java:325)
 04-30 14:43:25.625: W/ExternalStorage(14181):  at com.augmentedwine.view.ARActivity$1.run(ARActivity.java:254)
 04-30 14:43:25.625: W/ExternalStorage(14181):  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1434)
 04-30 14:43:25.625: W/ExternalStorage(14181):  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1217)

在这个函数中:

 void createExternalStoragePrivatePicture(String url) throws MalformedURLException,      IOException {

    File path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
    File file = new File(path, "LOL.jpg");

    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.connect();
    InputStream input = connection.getInputStream();


    try {

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

        Log.d("Size", "Tamanho " + is.available());


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

你能帮助我吗?非常感谢。

4

0 回答 0