3

在我的应用程序中,我可以选择拍摄/选择照片、音频或视频文件。我将每个文件路径写在它自己的字符串中并用它执行一些操作。另外,我用它来确定文件大小,方法如下:

    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;

由于 3 个原因,taken_pic_path 是 100% 正确的:1)。我使用相同的路径来创建预览并且它可以工作。2)。我让通过 Toast 显示的路径,它似乎是正确的 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();

        }

输出:正确的文件路径/true/true/0.0
什么鬼……

4

2 回答 2

0

尝试

double s = 1.0 * (double) getSize(taken_pic_path) / 1024.0 / 1024.0;
于 2012-12-20T18:30:17.157 回答
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)
于 2013-10-30T22:22:54.907 回答