Sending serialized file via other apps

Hello I made serialized object file and I want to send it via , messenger, gmail, sms, etc. I tried to send it in this way, this is code from saving to sending. I got that no one app is connected with this.

File file=new File(getFilesDir() + "ShoppingList.ur");
                try{
                    FileOutputStream fileOutputStream =new FileOutputStream(file);
                    ObjectOutputStream data=new ObjectOutputStream(fileOutputStream);
                    data.writeObject(toShare);
                    data.close();
                    fileOutputStream.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
                uriToSend=Uri.fromFile(file);
                Intent shareIntent=new Intent(Intent.ACTION_SENDTO);
                shareIntent.addCategory("*/*");
                shareIntent.putExtra(Intent.EXTRA_STREAM,uriToSend);
                startActivity(Intent.createChooser(shareIntent,getString(R.string.sendTo)));
                file.delete();

1 answer

  • answered 2017-06-17 18:34 CommonsWare

    Hello I made serialized object file and I want to send it via , messenger, gmail, sms, etc

    Do not assume that this file will be usable anywhere.

    I got that no one app is connected with this

    There are approximately zero apps that have an <activity> with an <intent-filter> with a <category> of */*.

    In addition:

    • ACTION_SENDTO requires a Uri (mailto: or smsto: schemes, usually)

    • You have not specified a MIME type for this file (e.g., application/octet-stream, since there is no official MIME type for it)

    • Do not use string concatenation to assemble file paths — replace new File(getFilesDir() + "ShoppingList.ur") with new File(getFilesDir(), "ShoppingList.ur")

    • You are trying to use the file even if you have an exception that did not create it

    • You are deleting the file before the other app will have a chance to do anything with it

    • Other apps do not have any access to your app's getFilesDir(), and so they will not be able to work with this file

    • Using a Uri with a file scheme (Uri.fromFile()) is banned on Android 7.0+ for apps with a targetSdkVersion of 24 or higher