将图像从firebase服务器下载到android内部存储时出现FileNotFoundException

时间:2017-08-07 11:22:54

标签: android firebase firebase-storage filenotfoundexception

我继续绊倒FileNotFoundException,即使一切似乎都正常,文件存在于指定的链接中并且连接良好。

以下是我从firebase服务器下载图片的代码段。

   btnDownload.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final String imageUrl= imageUri[0];
       // System.out.println("The Magazine Uri is: " + imageUri[0]);

        new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    FirebaseStorage storage = FirebaseStorage.getInstance();
                    // Creating a reference to the link
                    StorageReference httpsReference = storage.getReferenceFromUrl(imageUrl);
                    // Getting the Download url and conveting it to java.url
                    URL url = new URL(httpsReference.getDownloadUrl().toString());
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setDoOutput(true);
                    urlConnection.connect();



                    File file;
                    file=new File(mContext.getFilesDir(),"DownloadedImage.png");
                    OutputStream fileOutPut = new BufferedOutputStream(new FileOutputStream(file));
                    //FileOutputStream fileOutPut = new FileOutputStream(file);

                    InputStream inputStream = urlConnection.getInputStream();

                    int totalSize = urlConnection.getContentLength();
                    System.out.println("TotalSize :" + totalSize);

                    byte[] buffer = new byte[1024 * 1024];
                    int bufferLength = 0;
                    while ((bufferLength = inputStream.read(buffer)) > 0) {
                        fileOutPut.write(buffer, 0, bufferLength);
                        int downloadedSize = 0;
                        downloadedSize += bufferLength;
                        System.out.println("Downloaded Size :" + downloadedSize);
                    }
                    fileOutPut.close();
                } catch (final MalformedURLException e) {
                    System.out.println("Error : MalformedURLException " + e);
                } catch (final IOException e) {
                    System.out.println("Error : IOException " + e);
                    e.printStackTrace();
                } catch (final Exception e) {
                    System.out.println("Error : Please check your internet connection " + e);
                }
            }
        }).start();


    }
});

下面是错误日志:

  

08-07 14:16:29.280 1366-1386 /? W / audio_hw_generic:没有向HAL提供足够的数据,预期位置6153393,只写了6153120   08-07 14:16:32.505 1366-1387 /? W / audio_hw_generic:没有向HAL提供足够的数据,预期位置6460704,只写了6307920   08-07 14:16:40.573 3381-3545 /? I / System.out:错误:IOException java.io.FileNotFoundException:https://firebasestorage.googleapis.com/v0/b/whatsoutmain.appspot.com/o/februaryissue.png?alt=media&token=87602fe6-d7af-4ab4-b41e-59be7fb3deb1   08-07 14:16:40.573 3381-3545 /? W / System.err:java.io.FileNotFoundException:https://firebasestorage.googleapis.com/v0/b/whatsoutmain.appspot.com/o/februaryissue.png?alt=media&token=87602fe6-d7af-4ab4-b41e-59be7fb3deb1   08-07 14:16:40.573 3381-3545 /? W / System.err:at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)   08-07 14:16:40.573 3381-3545 /? W / System.err:at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)   08-07 14:16:40.573 3381-3545 /? W / System.err:at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)   08-07 14:16:40.573 3381-3545 /? W / System.err:at yenettaapp.whatsoutmain.DownloadPop $ 1 $ 1 $ 1.run(DownloadPop.java:108)   08-07 14:16:40.573 3381-3545 /? W / System.err:at java.lang.Thread.run(Thread.java:761)

似乎有什么问题?有什么方法可以解决这个问题?

1 个答案:

答案 0 :(得分:0)

从Firebase存储引用下载文件必须使用其他方法完成。使用下面的代码 -

FirebaseStorage storage = FirebaseStorage.getInstance();
// Creating a reference to the link
StorageReference httpsReference = storage.getReferenceFromUrl(imageUrl);

File file = null;

try {
    file=new File(mContext.getFilesDir(),"DownloadedImage.png");
} catch (IOException e) {
    e.printStackTrace();
}


httpsReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {

        System.out.println("kudos");
        // Local temp file has been created
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});