2016-11-15 88 views
0

我正在製作一個應用程序,用戶可以拍攝並上傳圖片,另一個用戶可以通過ImageView下載並查看該應用程序。爲了防止我佔用應用程序的所有內存,我將整個圖片放在存儲上。從Firebase下載的圖片獲取路徑?

它已在這裏與標準公式合作; https://firebase.google.com/docs/storage/android/download-files

問題是,我不知道如何獲取圖像下載到的路徑。我無法在AVD顯示器中找到它,並且我需要每個下載圖片的URL,因爲它是自定義的。我試着用這樣的東西來創建一個新的目錄並放在那裏,但它不起作用,我也無法獲得路徑。

public void getImageFromPath() throws IOException { 
      StorageReference islandRef = storageRef.child("userimages/[email protected]/[email protected]"); 

      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 

      String imageFileName = "JPEG_" + timeStamp + "_"; 

      File directory = new File(Environment.getDataDirectory() 
        + "/Test/"); 
      if (!directory.exists()) { 
       directory.mkdir(); 
      } 
      final File localFile = File.createTempFile(imageFileName, ".jpg", directory); 
      try { 
       islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { 
        @Override 
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { 
         Toast.makeText(getApplicationContext(), "Downloaded", Toast.LENGTH_SHORT).show(); 
         Log.d(TAG, "Downloaded til path"); 
         String mCurrentPhotoPath = "file:" + localFile.getAbsolutePath(); 
         System.out.println(mCurrentPhotoPath); 

        } 
       }).addOnFailureListener(new OnFailureListener() { 
        @Override 
        public void onFailure(@NonNull Exception exception) { 
         Log.d(TAG, "Downloaded IKKE til path"); 
        } 
       }); 
       throw new IOException("Works not"); 
      } 
      catch(IOException e) 
      { 
       System.out.println(e.getMessage()); 
      } 

     } 

回答

0

您正在創建臨時本地文件。這就是你下載數據的地方。

然後,您可以獲取該文件的絕對路徑有:

File localFile = File.createTempFile(imageFileName, ".jpg", directory); 
String path = localFile.getAbsolutePath(); 
+0

感謝您的答覆!現在我似乎得到了「打開失敗:ENOENT(沒有這樣的文件或目錄)」異常。 :(它發生在這些行中的字符串路徑之前;最終文件localFile = File.createTempFile(imageFileName,「.jpg」,directory); String path = localFile.getAbsolutePath(); System.out.println(「測試「+路徑); – NicklasN