Retrieving File Information

本文介绍如何通过FileProvide查询文件的MIME类型及大小。利用ContentResolver和Cursor对象,可以实现从文件URI中提取文件类型和尺寸,适用于Android应用开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以通过查询提供文件app的FileProvide来得到文件的MIME 和 size.
在得到文件的Uri后,可以调用ContentResolve.getType()来得到文件的类型.FileProvide是根据文件的后缀名来得到文件的类型.
 Uri returnUri = returnIntent.getData();
 String mimeType = getContentResolver().getType(returnUri);


FileProvide 的query方法可以返回文件的size和name.
FileProvide的返回值是一个Cursor对象,可以通过设定projection=null,selection=null,selectionArgs=null,sortOrder=null。来同时得到文件名和文件size,


Cursor query (Uri uri, 
                String[] projection, 
                String selection, 
                String[] selectionArgs, 
                String sortOrder)


下面是例子.
  Uri returnUri = returnIntent.getData();
    Cursor returnCursor =
            getContentResolver().query(returnUri, null, null, null, null);
    /*
     * Get the column indexes of the data in the Cursor,
     * move to the first row in the Cursor, get the data,
     * and display it.
     */
    int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
    int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
    returnCursor.moveToFirst();
    TextView nameView = (TextView) findViewById(R.id.filename_text);
    TextView sizeView = (TextView) findViewById(R.id.filesize_text);
    nameView.setText(returnCursor.getString(nameIndex));
    sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex)));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值