【问题标题】:Share file .txt Android分享文件 .txt Android
【发布时间】:2023-02-24 04:40:55
【问题描述】:

我正在尝试将 .txt 文件共享到另一个应用程序(例如 gmail),但我遇到了一些问题。我决定使用文件提供程序。当我在要与之共享文件的应用程序中选择 gmail 时,该应用程序向我返回“无法附加文件”toast。

我用这种方法在内部存储器中写入文件 txt:

public void esportaTxt(Graph graph, int id){
        final String FILE_NAME = id + "_TXT" + ".txt";
        List<Zona> zone = new ArrayList<>();
        Iterator<Zona> iterator = graph.vertexSet().iterator();

        zone = fromIteratorToArrayZone(iterator);

        File fileOutputFolder = new File(context.getFilesDir(), "fileOutput"); //cartella in cui salvare i file da condividere

        FileOutputStream fileOutputStream = null;
        try {
            fileOutputFolder.mkdirs(); //crea la cartella se non esiste
            File file = new File(fileOutputFolder, FILE_NAME); //il file da salvare

            fileOutputStream = new FileOutputStream(file);

            for(int i = 0; i < zone.size(); i++){
                fileOutputStream.write((i + 1 + ") " + zone.get(i).getNome() + "\n").getBytes());

                Iterator<Oggetto> iteratoreOggetti = zone.get(i).getListaOggetti().iterator();
                while (iteratoreOggetti.hasNext()){
                    fileOutputStream.write(("   - " + iteratoreOggetti.next().getNome() + "\n").getBytes());
                }
            }

            contentUri = FileProvider.getUriForFile(context, "com.example.eculturetool.fileprovider", file);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

我使用以下方法共享文件 txt:

public void shareFileTxt(int id){
        String fileName = id + "_TXT.txt";
        String stringFile = context.getFilesDir() +  "/fileOutput" +  File.separator + fileName;

        File file = new File(stringFile);
        contentUri = FileProvider.getUriForFile(context, "com.example.eculturetool.fileprovider", file);

        if(!file.exists()){
            Toast.makeText(context, "Il file non esiste!", Toast.LENGTH_LONG).show();
            return;
        }


        Intent intentShare = new Intent(Intent.ACTION_SEND);
        intentShare.setType("text/*");
        intentShare.putExtra(Intent.EXTRA_SUBJECT, "Subject Here"); //per condividere con email app
        intentShare.putExtra(Intent.EXTRA_STREAM, contentUri);
        intentShare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(Intent.createChooser(intentShare, "Condividi file"));
    }

我在活动中需要的权限:

ActivityCompat.requestPermissions(this, new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, PERMISSION_GRANTED);
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());

android清单中的提供者:

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.eculturetool.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

Gmail的画面

【问题讨论】:

  • 为什么导出的标签是假的?
  • 来自文档:将 android:exported 属性设置为 false; FileProvider 不需要公开。
  • 好的。但是,我对intentShare.setType("text/*"); 有点怀疑。可能是我错了
  • The permissions that I require in the Activity: getFilesDir() 不需要该权限。
  • 尝试不使用选择器。

标签: java android file share sharing


【解决方案1】:

你必须在启动 intentShare 之前授予 uri 权限, 像这样的代码:

context.grantUriPermission(
                "your.package.name",
                contentUri,
                Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
            )

【讨论】:

    【解决方案2】:

    我遇到了同样的问题并花了很多时间。但是解决方案不在代码中。 在我的小米设备上,“MIUI 优化”设置被禁用。它位于开发人员选项的底部。启用它后,文件共享开始完美运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多