1. 程式人生 > >provider標籤內容提供器

provider標籤內容提供器

只要不懂就會入坑,就算是看起來理所當然,也是一樣的。

首先是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 uriUri 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();