首先介绍一点背景知识:此应用程序拍摄照片并上传到 Azure Blob 存储。
图片存储在文件(内部存储)中使用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();
其中文件长度为 0。
我已经针对使用进行了测试Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
为存储目录。这有效。
我需要使用内部存储与外部存储,因为这是该项目特别需要的。
编辑:安卓File文档没有提及此行为。我假设这可能与使用应用程序内部存储有关。
编辑:添加一些代码
我正在发送一个FilemPhotoFile 到我的相机意图。这将包含照片。 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
我必须将此文件的路径传递给从文件上传功能。
我的“答案”中提到的解决方法。