Home > Software design >  How share/export a JSON file in Java Android
How share/export a JSON file in Java Android

Time:04-22

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

CodePudding user response:

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"));
  • Related