2012-12-20 92 views
3

在我的應用程序中,我可以選擇拍攝/選擇照片,音頻或視頻文件。我將每個文件路徑寫入其自己的字符串中,並對其執行一些操作。另外,我用它來確定與下面的方法將文件大小:android無法獲取拍攝照片的大小..一個錯誤?

public double getSize(String path) { 

    File file = new File(path); 

    if (file.exists()) { 
     long size = file.length(); 

     return size; 
    } else { 
     Log.e("zero size", "the file size is zero!"); 
     return 0; 

    } 

} 

它工作正常,但該方法總是返回0,而試圖讓所拍攝圖片的大小。

double s = 1.0 * getSize(taken_pic_path)/1024/1024; 

taken_pic_path 100%正確的原因有3個:1)。我使用相同的路徑來創建預覽,它的工作原理。 2)。我讓路徑通過吐司顯示,它似乎是正確的3)。我用file.exists()檢查路徑,它返回true。我也試過如下:

File file = new File(taken_pic_path); 
if (file.exists()){ 
    double test = file.lenght(); 
} 

我總是得到一個零文件大小..相同的技術完美地與拍攝的視頻,音頻拍攝,選擇的圖片/視頻/音頻,我只得到0,而試圖獲取拍攝照片的大小。只是不能得到原因..任何想法?

編輯 我做了一切可能的檢查:

if (file.exists()) { 

      String b = file.getPath(); 
      boolean r = file.canRead(); 
      boolean w = file.canWrite(); 
      double d = file.length(); 
      Toast.makeText(getApplicationContext(), b, Toast.LENGTH_LONG) 
        .show(); 
      Toast.makeText(getApplicationContext(), Boolean.toString(r), 
        Toast.LENGTH_LONG).show(); 

      Toast.makeText(getApplicationContext(), Boolean.toString(w), 
        Toast.LENGTH_LONG).show(); 
      Toast.makeText(getApplicationContext(), Double.toString(d), 
        Toast.LENGTH_LONG).show(); 

     } 

OUTPUT:正確的文件路徑/真/真/ 0.0
什麼是地獄......

+0

爲什麼會返回一個雙? – njzk2

+0

它是0之前,你分裂它? – njzk2

+0

我被告知在前一個主題中使用double,否則當結果小於1時,我會得到一個零。然後我使用一種方法將其舍入到2位小數。是的,它已經是零。 getSize總是返回0用於該路徑 – Droidman

回答

0

嘗試

double s = 1.0 * (double) getSize(taken_pic_path)/1024.0/1024.0; 
+0

相同的結果。試過 File file = new File(taken_pic_path); \t \t \t \t \t \t如果(file.exists()){ \t \t \t \t Toast.makeText(getApplicationContext(), 「文件是存在的!」,Toast.LENGTH_LONG).show(); \t \t \t \t s = 1.0 *(double)getSize(taken_pic_path)/ 1024.0/1024.0;\t Toast.makeText(getApplicationContext(),Double.toString(s),Toast.LENGTH_LONG).show(); \t \t \t} 結果:文件在那裏! 0.0 – Droidman

+0

該文檔說:File.length():此抽象路徑名錶示的文件的長度(以字節爲單位),如果文件不存在,則爲0L。它看起來像你沒有妥善保存圖像。 – alistair

+0

圖像由相機應用程序自動保存。如果不是,我將無法創建一個位圖用作相同路徑的預覽,但事實並非如此。位圖已正確創建 – Droidman

0

只是偶然發現了同樣的問題。對於某些設備,當Android媒體提供商通知有關添加到提供商的新內容時,該文件仍報告長度爲零。僅在半秒後才報告正確的長度。

我會認爲這是一個錯誤。

我的解決辦法是那麼難看,因爲這:

/** 
* Careful: this method may take a few seconds to complete for some photos. 
* <p> 
* Do not attempt to read {@link Images.Media.SIZE}, since it is known to return 
* wrong results (up to 5% off). We used to use it until 2.2.0. 
* 
* @return the file size, or zero if the file does not exist 
* @see #isFileReallyAvailable 
*/ 
private static long getFileSize(File file) { 
    if (!file.exists()) { 
     return 0; 
    } else { 
     if (file.length() > 0) { 
      return file.length(); 
     } else if (isFileReallyAvailable(file)) { 
      return file.length(); 
     } else { 
      Log.w(TAG, "The file " + file.getName() + " was not available. Will report size 0"); 
      return 0; 
     } 
    } 
} 

/** 
* Is this file really available to be read? That is, does it have a non-zero length? 
* <p> 
* Seems like 
* some photo-taking apps (even the standard one in some devices) notify the Media provider 
* too soon, so {@code file.getLength()} is still zero. In those cases, we will retry 
* a few times after some sleeps. Related story: #59448752 
* <p> 
* Careful: this method may take a few seconds to complete for some photos, so do not call 
* it from the GUI thread. 
* <p> 
* Also, do not attempt to read {@link Images.Media.SIZE}, since it is known to return 
* wrong results (up to 5% off). We used to use it until 2.2.0. 
* 
* @return the file size, or zero if the file does not exist 
*/ 
private static boolean isFileReallyAvailable(File file) { 
    boolean available = false; 
    final long timeout = 2000; // In the Nexus 4 I tried, 700 ms seems like the average 
    final long sleepEveryRetry = 100; 
    long sleepSoFar = 0; 
    do { 
     try { Thread.sleep(sleepEveryRetry); } catch (Exception e) {} 
     final long fileSize = file.length(); 
     available = fileSize > 0; 
     sleepSoFar += sleepEveryRetry; 
     Log.v(TAG, "The file " + file.getName() + " is still not valid after " + sleepSoFar + " ms"); 
    } while (!available && sleepSoFar < timeout); 

    return available; 
} 

這些都是這產生我唯一的手機上,我可以重現這個(運行4.3的Nexus 4)日誌:

10-30 18:17:03.324: V/Scanner(21167): The file IMG_20131030_181702.jpg is still not valid after 100 ms 
10-30 18:17:03.424: V/Scanner(21167): The file IMG_20131030_181702.jpg is still not valid after 200 ms 
10-30 18:17:03.524: V/Scanner(21167): The file IMG_20131030_181702.jpg is still not valid after 300 ms 
10-30 18:17:03.634: V/Scanner(21167): The file IMG_20131030_181702.jpg is still not valid after 400 ms 
10-30 18:17:03.734: V/Scanner(21167): The file IMG_20131030_181702.jpg is still not valid after 500 ms 
10-30 18:17:03.774: V/Scanner(21167): Found a new undiscovered image in the local phone: upload wrapper for IMG_20131030_181702.jpg (ID = <none>, 25 KB, JUST_DISCOVERED) (source item ID is 3126) 

10-30 18:17:06.537: V/Scanner(21167): The file IMG_20131030_181705.jpg is still not valid after 100 ms 
10-30 18:17:06.637: V/Scanner(21167): The file IMG_20131030_181705.jpg is still not valid after 200 ms 
10-30 18:17:06.737: V/Scanner(21167): The file IMG_20131030_181705.jpg is still not valid after 300 ms 
10-30 18:17:06.837: V/Scanner(21167): The file IMG_20131030_181705.jpg is still not valid after 400 ms 
10-30 18:17:06.937: V/Scanner(21167): The file IMG_20131030_181705.jpg is still not valid after 500 ms 
10-30 18:17:07.038: V/Scanner(21167): The file IMG_20131030_181705.jpg is still not valid after 600 ms 
10-30 18:17:07.058: V/Scanner(21167): Found a new undiscovered image in the local phone: upload wrapper for IMG_20131030_181705.jpg (ID = <none>, 24 KB, JUST_DISCOVERED) (source item ID is 3127) 

10-30 18:17:07.969: V/Scanner(21167): The file IMG_20131030_181707.jpg is still not valid after 100 ms 
10-30 18:17:08.069: V/Scanner(21167): The file IMG_20131030_181707.jpg is still not valid after 200 ms 
10-30 18:17:08.169: V/Scanner(21167): The file IMG_20131030_181707.jpg is still not valid after 300 ms 
10-30 18:17:08.289: V/Scanner(21167): The file IMG_20131030_181707.jpg is still not valid after 400 ms 
10-30 18:17:08.389: V/Scanner(21167): The file IMG_20131030_181707.jpg is still not valid after 500 ms 
10-30 18:17:08.499: V/Scanner(21167): The file IMG_20131030_181707.jpg is still not valid after 600 ms 
10-30 18:17:08.509: V/Scanner(21167): Found a new undiscovered image in the local phone: upload wrapper for IMG_20131030_181707.jpg (ID = <none>, 20 KB, JUST_DISCOVERED) (source item ID is 3128)