简体   繁体   English

通过意图从图库中拾取的图像的长度

[英]Length of image picked from gallery via intent

I have a problem. 我有个问题。 I want to make file uploader app. 我要制作文件上传器应用。 I'm using Intent.ACTION_GET_CONTENT to pick any file from my device. 我正在使用Intent.ACTION_GET_CONTENT从设备中选择任何文件。 I am receiving the file in onActivityResult like that: 我在onActivityResult中收到这样的文件:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CHOOSER:
            if (resultCode == RESULT_OK) {
                final Uri uri = data.getData();
                File file = FileUtils.getFile(uri);
                String fileName = file.getName();
                String filePath = file.getPath();
                int fileSize = Integer.parseInt(String.valueOf(file.length()/1024));

                tvName.setText(fileName);
                tvPath.setText(filePath);
                tvSize.setText(String.valueOf(fileSize) + "kB");
            }
    }
}

I want to show user information about picked file. 我想显示有关所选文件的用户信息。 In general everything is fine, but when I choose Image from gallery then: 总的来说,一切都很好,但是当我从Gallery中选择Image时:

  1. The file name shows no format - it's a number (probably reference to file in memory). 文件名未显示格式-它是一个数字(可能是内存中的文件引用)。 When I pick Image via installed file explore I get sth like imagename.png etc, but picked from gallery is like "195493" 当我通过安装的文件浏览器选择“图像”时,我会得到诸如imagename.png等的信息,但是从图库中选择的则是“ 195493”
  2. File.length created from data picked from gallery is 0. 从库中选取的数据创建的File.length为0。

Is there any way to access real image name and size after picking image from Gallery via intent? 通过意图从图库中选择图像后,是否可以访问真实的图像名称和大小?

When picking images from the gallery, you don't get a reference to the actual file but to a row in the database. 从图库中选择图像时,您不会引用实际文件,而是引用数据库中的一行。 You have to retrieve the actual file path with a Cursor like in this answer . 您必须使用此答案中的Cursor检索实际文件路径。

Just input URI from the intent and get the size of any file 只需从意图中输入URI并获取任何文件的大小

uri = data.getData();
        Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
        returnCursor.moveToFirst();
        Log.e("TAG", "Name:" + returnCursor.getString(nameIndex));
        Log.e("TAG","Size: "+Long.toString(returnCursor.getLong(sizeIndex)));

I think this will help you. 我认为这会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM