2

I created a code which generates a JSON file and saves in /data/data/ path, and I want to get the selected file and share/export. I'm trying to use Intent, follow the code:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        File fileItem = itens.get(position);

        holder.textFileName.setText(fileItem.getName());

        holder.shareImage.setOnClickListener(v ->  {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("application/json");
            intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/" + fileItem));

            context.startActivity(Intent.createChooser(intent, "Compartilhar log de auditoria"));
        });

    }

But when I select to save the media like in File Manager and select the folder shows "Incorrect Path" and in all apps shows the attachment is incompatible. Help!

OBS: I am implementing the code in RecyclerView class

2
  • 1
    You should be crashing with a FileUriExposedException. Uri.fromFile() has been effectively banned for nearly six years. Use FileProvider to share files with other apps. Commented Apr 19, 2022 at 21:09
  • thanks for the help @CommonsWare i changed to FileProvider and now its working Commented Apr 19, 2022 at 21:47

1 Answer 1

2

After the comment of @CommonsWare, i used the FileProvider and now i can share/export a JSON file, follow the working code:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/json");

Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, fileItem);

intent.putExtra(Intent.EXTRA_STREAM, uri);

context.startActivity(Intent.createChooser(intent, "Exportar arquivo de Log de Auditoria"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.