使用FileProvider共享文件

最近学习FileProvider,不知为何,汉语资料很少。所以就自己啃官方文档,实现了一遍。
官方文档路径:
需要一下几步
一、在共享端设置

首先,需要在 Menifest里添加 provider 标签,
<!-- 在这里定义共享信息 -->
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.fileproviderdemo.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true" >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>



然后,指定共享目录
     在res目录下,新建目录xml,在xml下新建filepaths.xml 文件
<?xml version="1.0" encoding= "utf-8"?>
<resources>
    <paths >
      <files-path path="files/" name="intfiles" />
      <external-path path="files/" name="extfiles" />
    </paths >
</resources>

     别忘了外部路径需要权限。

二、创建一个文件选择Activity。
        <!-- 文件选择页面ACTIVITY -->
        <activity
            android:name=".FileSelectActivity"
            android:label="@string/app_name" >
            <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>


三、在代码中响应

       
/*当条目被点击时,设置setResult,将选择的结果返回给调用此activity的activity*/
       @Override
       public boolean onChildClick (ExpandableListView parent, View v,
                   int groupPosition, int childPosition, long id) {
            /*得到点击的File*/
            File file = ((File[])mGroupMap.get(mGroups [groupPosition]))[childPosition];
            Uri fileUri;
            /*authority 必须和Menifest的provider标签里定义的一致*/
            String authority = getResources().getString(R.string.fileprovider_authority );
             try {
                 fileUri = FileProvider. getUriForFile(
                    FileSelectActivity. this,
                    authority,
                    file);
            Intent resultIntent = new Intent();
            if (fileUri != null) {
                  resultIntent.addFlags(
                         Intent. FLAG_GRANT_READ_URI_PERMISSION );
                // Put the Uri and MIME type in the result Intent
                  resultIntent.setDataAndType(
                        fileUri,
                        getContentResolver().getType(fileUri));
                // 设置返回结果状态,
                  setResult(Activity. RESULT_OK,
                              resultIntent);
            } else {
                  resultIntent.setDataAndType( null, "" );
                  setResult( RESULT_CANCELED,
                              resultIntent);
            }                        
            finish();                      
        } catch (Exception e) {
            e.printStackTrace();                         
        }
             finally{
                  finish();
            }
             return true ;
      }


四、在请求端,也就是请求共享的App的代码中,发起请求
Intent intent = new Intent(Intent.ACTION_PICK);
                 intent.setType( "text/plain");
                 startActivityForResult(intent, 0);


     被调用Activity返回前,调用setResult方法,设置了 RESULT_OK,在被调用Activity结束后,系统会调用 onActivityResult方法
          得到之前设置的RESULT_OK状态,表示成功获取到文件。如果获取失败,则应该设置RESULT_CANCEL。
             @Override
             public void onActivityResult(int requestCode, int resultCode,
                        Intent data) {
                   // TODO Auto-generated method stub
                   if (resultCode != RESULT_OK) {
                         tvFileContent.setText("未成功获得文件" );
                         tvFileName.setText("未成功获得文件" );
                  } else {
                        readFile(data.getData());
                  }
            }
            
             private void readFile(Uri returnUri) {
                  Context context = getActivity();
                  ParcelFileDescriptor inputPFD;
                   //获取文件句柄
                   try {                
                        inputPFD = context.getContentResolver().openFileDescriptor(returnUri, "r" );
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                tvFileContent.setText("获取文件句柄失败" );
                tvFileName.setText("获取文件句柄失败" );
                return;
            }
                  
                   //获取文件名字和大小
                   Cursor returnCursor =
                        context.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();
                tvFileName.setText("文件名:" +returnCursor.getString(nameIndex)+", 大小:"+
                Long. toString(returnCursor.getLong(sizeIndex))+" B");
                  returnCursor.close();
                  
                   //读取文件内容
                  String content = "";
                  FileReader fr = null;
                   char[] buffer = new char[1024];
                  
                   try {
                        StringBuilder strBuilder = new StringBuilder();
                        fr = new FileReader(inputPFD.getFileDescriptor());
                         while (fr.read(buffer) != -1) {
                              strBuilder.append(buffer);
                        }
                        fr.close();
                        content = strBuilder.toString();
                  } catch (Exception e) {
                         // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
                  
                   if (content.length() != 0) {
                         tvFileContent.setText(content);
                  } else {
                         tvFileContent.setText("<内容空>" );
                  }
                  
                   try {
                        inputPFD.close();
                  } catch (IOException e) {
                         // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
            }
      }

代码下载
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值