2014-04-30 29 views
1

我试图让我的应用程序通过邮件,电报或任何其他可以管理一般文件的应用程序发送二进制文件。在Android中共享二进制文件

代码:

@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; 


} 

当我选择的邮件应用程序,例如,送它,它告诉我,“不能添加此附件”。为什么会这样?

+0

http://stackoverflow.com/questions/4646913/android-how-use-use-mediascannerconnection-scanfile – samosaris

回答

-1

这是我写的方法,它通常适用于图像和二进制附件。我正在使用Android Jelly Beans。请参阅下面的代码。可能是您的邮件所需的额外字段。

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; 
} 
0
File f = new File(getFilesDir(),circuit.getName() + ".obj"); 

getFilesDir()返回应用程序私有目录。没有其他应用程序可以访问此目录。此外,电子邮件应用程序无法访问它。

您必须将文件复制到公共目录,以便电子邮件应用程序可以访问它。使用Environment.getExternalStorageDirectory()获取公共目录。您的电话会看起来像这样:

File f = new File(Environment.getExternalStorageDirectory(),circuit.getName() + ".obj"); 
相关问题