2012-12-20 61 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)