1

I'm trying to let my application send a binary file through mail, telegram or any other app which can manage general files.

The code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.circuit_menu, menu);
    MenuItem item = menu.findItem(R.id.menu_item_share);

    // Fetch and store ShareActionProvider
    ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider();

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("*/*");
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    File f = new File(getFilesDir(),circuit.getName() + ".obj");
    if(f.exists()){
        Log.d("FILE",f.getAbsolutePath());//Checking
    }
    Uri uri = Uri.parse(f.getAbsolutePath());
    Log.d("URI",uri.toString());//Checking
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    Intent.createChooser(shareIntent, "Share via");
    mShareActionProvider.setShareIntent(shareIntent);
    return true;


}

When I chose the mail application,for example, to send it, it told me that "Can not add this attachment".Why is that?

Xhark
  • 781
  • 1
  • 9
  • 24
  • http://stackoverflow.com/questions/4646913/android-how-to-use-mediascannerconnection-scanfile – samus Apr 18 '17 at 13:23

2 Answers2

0
File f = new File(getFilesDir(),circuit.getName() + ".obj");

getFilesDir() returns the applications private directory. No other application can access this directory. Also the email application cannot access it.

You would have to copy your file to a public directory so that the email application can access it. You get the public directory with Environment.getExternalStorageDirectory(). Your call would then look like:

File f = new File(Environment.getExternalStorageDirectory(),circuit.getName() + ".obj");
Oliver Kranz
  • 3,741
  • 2
  • 26
  • 31
-1

This is method I wrote which works generically for Image and Binary attachments. I am using Android Jelly Beans where it works. See the code below. May be your missing extra fields required for mail.

 public static  void shareImagesIntent(ArrayList<Uri> imageUris,Context context,String SubjectTitle,String MessageBody)
 {
     if(imageUris == null || imageUris.size()==0)
     {
         return;
     }
     Intent shareIntent = new Intent();
     Uri uri = imageUris.get(0);

     shareIntent.putExtra(Intent.EXTRA_TEXT,MessageBody);
     shareIntent.putExtra(Intent.EXTRA_TITLE, SubjectTitle);
     shareIntent.putExtra(Intent.EXTRA_SUBJECT, SubjectTitle);
     shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
     shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
     shareIntent.setType("image/*");

     context.startActivity(Intent.createChooser(shareIntent, MessageBody));
     return;
 }
NEERAJ SWARNKAR
  • 427
  • 4
  • 9