从URI或原始文件路径重新创建文件时文件长度为0

首先介绍一下背景:此应用程序拍摄照片并上传到Azure blob存储。

图片使用getApplicationContext().getFilesDir();存储在文件(内部存储)中getApplicationContext().getFilesDir();

要上传,我需要调用uploadFromFile(..)函数,如下所示: CloudBlockBlob.uploadFromFile(String path);

Azure SDK的uploadFromFile函数如下所示:

 public void uploadFromFile(final String path) throws StorageException, IOException { uploadFromFile(path, null /* accessCondition */, null /* options */, null /* opContext */); } public void uploadFromFile(final String path, final AccessCondition accessCondition, BlobRequestOptions options, OperationContext opContext) throws StorageException, IOException { File file = new File(path); long fileLength = file.length(); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); this.upload(inputStream, fileLength, accessCondition, options, opContext); inputStream.close(); } 

问题是在行long fileLength = file.length(); fileLength为0的位置。

我已经使用Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);对此进行了测试Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 对于存储目录。 这很有效。

我需要使用内部存储与外部存储,因为这是该项目特别需要的。

编辑:Android 文件文档没有提到这种行为。 我假设这可能与使用应用程序内部存储有关。

编辑:添加一些代码

我正在向我的相机意图发送文件 mPhotoFile。 这将包含照片。 mPhotoFileUri包含此文件的URI。 以下是使用文件路径的代码。

 File file = new File(mPhotoFile.getPath()); // value -> /data/user/0/com.example.devpactapp/files/JPEG_20160209_234929_1936823724.jpg boolean fileExists = file.exists(); // true long fileLength = file.length(); // length 0 

以下是从URI获取文件的代码。

 File file = new File(mPhotoFileUri.getPath()); // value -> /data/user/0/com.example.devpactapp/files/JPEG_20160209_235534_-1059496729.jpg boolean fileExists = file.exists(); // true long fileLength = file.length(); // length 0 

我必须将此文件的路径传递给uploadFromFile函数。

在我的’回答’中提到的解决方法。

即使我没有所有的信息,我也会打电话给你提供的路径中缺少文件。 长度()的javadoc特别提到这种情况将返回0。

因此,在对文件执行任何操作之前,请尝试检查exists()。

使用内部存储时,文件路径不同。 我认为它存储在/ storage /模拟最新设备中。 检查文件路径。 并查看您是否使用绝对路径创建文件。

找到了解决方法。 这是我用来获取传递给Camera意图的文件的函数。

  // Create a File object for storing the photo private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getApplicationContext().getFilesDir(); mPhotoFileExists = true; // check if access to external storage exists // if returned true, return the new file // if returned false, user could have been requested for access // so, need to check if permission is available now // if still not available, return null if(haveWritePermissions()) return File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); return null; } 

我从getApplicationContext().getFilesDir();更改了storageDir.getFilesDir getApplicationContext().getFilesDir(); to getApplicationContext.getExternalFilesDir(null);

文档: getExternalFilesDir(String type) , getFilesDir()

我传递null因为我不想放置Environment.DIRECTORY_PICTURES ,这将允许Media Scanner找到图像文件。

我知道这绝不是一个“修复”。 仍在寻找关于为什么getFilesDir()导致此问题的答案。

编辑:非常紧迫的原因,为什么这不是一个好的解决方案 – 外部存储可能并不总是可用,并且在这种情况下将没有任何解决方法。 此外,任何其他具有WRITE_EXTERNAL_STORAGE权限的应用程序WRITE_EXTERNAL_STORAGE可以在此处写入。 因此,也没有强制执行安全性。