android开发仿照第三方app打开方式,如何将你的app显示在打开列表,并且将文件复制到你的app应用内

        最近在帮朋友做一个项目,有关文件处理的。之前提出的一个需求就是,用户在手机里随便点开一个文件,开发方式里要有自己开发的app,并且将这个文件复制到app所在文件夹里,在app应用里直接预览文件。这个就和第三方office app类似的。这个功能主要有2个难点:1是怎么把自己开发的app显示在打开列表里 2 如何复制所选择的文件拷贝到开发的app文件夹里。

    思路:1 .app显示在打开列表中,需要配置清单文件AndroidManifest.xml中activity里的intent-filter;2:文件复制到开发的app里用到里Uri。

  1.AndroidManifest.xml

    

<intent-filter>

    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.DEFAULT" />

    <data android:mimeType="*/*"/>

</intent-filter>
   这段配置代码一般配置在启动activity页面里。其中 <data android:mimeType="*/*"/>是选择文件格式,我这里默认是所有格式都能显示在打开列表里,如果需要开发指定
文件格式修改mimeType的内容,网上有很多我就不一一列举了,自行百度。
 
 
   2.Uri  
 
 
String action=getIntent().getAction();
//将文件复制到制定目录中
     if(Intent.ACTION_VIEW.equals(action)){
       String str = i.getDataString();
       Log.e("uri", str);
           if(str != null){
               Uri uri = null;
               uri = Uri.parse(str);//uri路径
         String filePath= FileUtil.getRealFilePath(this,uri);//获取文件绝对路径
             try {
               fileName=FileUtil.getFileName(filePath);
               InputStream in = this.getContentResolver().openInputStream(uri);//io 
               FileOutputStream out = new FileOutputStream(new File(path));//文件输出到开发app路径
               byte[] b = new byte[1024];
               try {
                  while ((in.read(b)) != -1) {
                     out.write(b);
                  }
                  in.close();
                  out.close();
               } catch (Exception e) {
                  e.printStackTrace();
               }
            }catch (Exception e){
               e.printStackTrace();
            }

      }
   


猜你喜欢

转载自blog.csdn.net/mrpanda87/article/details/73739874
今日推荐