从URI或原始文件路径重新创建文件时文件长度为0 [getExternalFilesDir(String type)vs getFilesDir()] [英] File length is 0 when recreating file from URI, or original filepath [getExternalFilesDir(String type) vs getFilesDir()]

查看:1447
本文介绍了从URI或原始文件路径重新创建文件时文件长度为0 [getExternalFilesDir(String type)vs getFilesDir()]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A little bit of background first: this app takes a picture and uploads to an Azure blob storage.

图片存储在一个文件(内部存储)中,使用 getApplicationContext()。getFilesDir();

The picture is stored in a file (internal storage) using getApplicationContext().getFilesDir();

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

To upload, I need to call the uploadFromFile(..) function like so: CloudBlockBlob.uploadFromFile(String path);

Azure SDK的uploadFromFile函数如下所示:

The Azure SDK's uploadFromFile function looks like this:

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.

The issue is on the line long fileLength = file.length(); where the fileLength is 0.

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

I have tested this against using Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); for the storage directory. This works.

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

I need to use the internal storage vs external storage since that is specifically required for this project.

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

The Android File documentation mentions nothing about this behavior. I am assuming this might be something to do with using the app internal storage.

编辑:添加一些代码

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

I am sending a File mPhotoFile to my Camera intent. This will contain the photo. mPhotoFileUri contains the URI for this file. Following is the code to use the path from the file.

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获取文件的代码。

Following is the code to get the file from the 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 功能。

I have to pass the path of this file to the uploadFromFile function.

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

Workaround mentioned in my 'answer'.

推荐答案

即使我没有所有的信息,我也会打电话给您给出的路径中缺少该文件。 javadoc for length()具体提及此案例将返回0。

Even though I don't have all the information, I'm gonna make the call that the file is missing from the path you've given. The javadoc for length() specifically mentions this case will return 0.

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

So try checking exists() before you do anything with the file.

这篇关于从URI或原始文件路径重新创建文件时文件长度为0 [getExternalFilesDir(String type)vs getFilesDir()]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆