【问题标题】:android cannot get size of taken photo.. a bug?android 无法获取所拍照片的大小.. 一个错误?
【发布时间】:2012-12-20 15:22:02
【问题描述】:

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

    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
什么鬼……

【问题讨论】:

  • 分之前是0吗?
  • 我被告知在上一个主题中使用双精度,否则当结果小于一时我会得到零。然后我使用一种方法将其四舍五入到小数点后两位。是的,它已经是零了。 getSize 始终返回与该路径一起使用的 0
  • 小于一个?长度以字节为单位,小于 1 字节的文件似乎不太可能......
  • 当然。刚拍了一张:/mnt/sdcard/DCIM/Camera/1356018878923.jpg
  • 当您单步执行时,getSize() 是因为文件大小报告为零还是因为 file.exists() 返回 false 而返回零?两条路径都将返回零。顺便说一句,不需要双倍。 file.length() 返回一个长整数。

标签: android file


【解决方案1】:

试试

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

【讨论】:

  • 同样的结果。尝试过 File file = new File(taken_pic_path); if(file.exists()){ Toast.makeText(getApplicationContext(), "文件在那里!", Toast.LENGTH_LONG).show(); s = 1.0 * (double) getSize(taken_pic_path) / 1024.0 / 1024.0; Toast.makeText(getApplicationContext(), Double.toString(s), Toast.LENGTH_LONG).show(); } 结果:文件在那里! 0.0
  • 文档说: File.length():这个抽象路径名表示的文件的长度,以字节为单位,如果文件不存在,则为 0L。您似乎没有正确保存图片。
  • 图像由相机应用程序自动保存。如果不是这样,我将无法从同一路径创建位图以用作预览,但事实并非如此。位图创建正确
【解决方案2】:

偶然发现了同样的问题。对于某些设备,当 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)

【讨论】:

    猜你喜欢
    • 2013-09-30
    • 1970-01-01
    • 2013-01-20
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    相关资源
    最近更新 更多