provider标签内容提供器

本文详细介绍了Android中使用FileProvider进行文件共享的方法。包括如何配置AndroidManifest.xml文件、创建XML路径映射文件、实现文件选择Activity及通过FileProvider获取文件URI等内容。

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

只要不懂就会入坑,就算是看起来理所当然,也是一样的。

首先是res/xml文件夹下的.xml文件

测试好多遍了,最后发现是这么回事,

getFilesDir()  data/data/com.example.li.myfileprovider/files

content uri   uri=content://com.example.li.myfileprovider.fileprovider/myimages/file.txt

<paths>
    <files-path
        name="myimages" path="files/">
        <!--文本文件路径要用files-->

    </files-path>
</paths>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.li.myfileprovider">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".FileSelectActivity"
            android:label="File Selector">
            <intent-filter>
                <action android:name="android.intent.action.PICK"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.OPENABLE" />

                <data android:mimeType="text/plain" />
                <data android:mimeType="image/*" />
            </intent-filter>

        </activity>
        <provider
            android:authorities="com.example.li.myfileprovider.fileprovider"
            android:name="android.support.v4.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths">

            </meta-data>
        </provider>
    </application>

</manifest>
文件选择Activity
public class FileSelectActivity extends Activity {


    private File mImageDir;
    private List<File> mImageFiles;
    private String[] mImageFileName;
    private Intent mResultIntent;
    private ListView listView;
    private ArrayAdapter<String> adapter;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      /* */

        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);
        button = (Button) findViewById(R.id.ok);

        mResultIntent=new Intent("com.example.li.myfileprovider.ACTION_RETURN_FILE");
        listView = (ListView) findViewById(R.id.list_view);
        List<String> data=getStrings();
        if (data!=null) {
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
            listView.setAdapter(adapter);

        }
        clickListView();
        setResult(Activity.RESULT_CANCELED,    null);
    }
    private List<String> getStrings(){
        List<String> list = new ArrayList<>();
        mImageFiles = new ArrayList<>();
        ///data/data/com.example.li.myfileprovider/files  getFilesDir();
        File dir = new File(getFilesDir(), "files");

        if (dir.exists()) {
            File []listFiles=dir.listFiles();
            for (File file : listFiles) {
                mImageFiles.add(file);
                String name=file.getName();
                list.add(name);

            }
            Log.d("test", "dir success");
            return list;
        }else {
            Log.d("test", "dir failed");
            return null;
        }


    }
    public void clickListView(){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                File selectedFile = mImageFiles.get(i);
                Log.d("test", selectedFile.getAbsolutePath());
/*		
getFilesDir()  data/data/com.example.li.myfileprovider/files
Uri uri = Uri.parse("content://com.example.li.myfileprovider.fileprovider/myimages/" + selectedFile.getName() );*/ //content://com.example.li.myfileprovider.fileprovider/myimages/files3.txt uri Uri uri = FileProvider.getUriForFile(FileSelectActivity.this, "com.example.li.myfileprovider.fileprovider", selectedFile); Log.d("test", uri.toString() + " uri"); if (uri != null) { mResultIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//零时可读,退出关闭 mResultIntent.setDataAndType(uri, getContentResolver().getType(uri)); FileSelectActivity.this.setResult(Activity.RESULT_OK,mResultIntent); }else { mResultIntent.setDataAndType(null, ""); FileSelectActivity.this.setResult(Activity.RESULT_CANCELED,mResultIntent); } ///data/data/com.example.li.myfileprovider/files/images/images3.txt } }); } public void onClick(View view) { finish(); }}
GetFileDemo
Cursor cursor = getContentResolver().query(returnUri, null, null, null, null);
/*String fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
long fileSize = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));*/
cursor.moveToFirst();//查询的结果curson要移到第一行
int namecolumn = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizecolumn = cursor.getColumnIndex(OpenableColumns.SIZE);
Log.d("test", cursor.getPosition()+"first");
/*cursor.moveToFirst();*///确保在第一行

String fileName = cursor.getString(namecolumn);
long fileSize = cursor.getLong(sizecolumn);
Log.d("test", cursor.getPosition() + "second");
name.setText("name"+fileName);
/*cursor.moveToFirst()*/
cursor.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值