android无法获取拍照的大小..一个bug?

时间:2012-12-20 15:22:02

标签: android file

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

    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)。我让路径通过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
怎么了......

2 个答案:

答案 0 :(得分:0)

尝试

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

答案 1 :(得分:0)

偶然发现了同样的问题。对于某些设备,当Android Media Provider通知添加到提供商的新内容时,该文件仍会报告长度为零。只有在半秒左右才能报告正确的长度。

我认为这是一个错误。

我的解决办法非常难看:

/**
 * 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)