通过其他应用程序共享文件

问题描述:

我想通过谷歌驱动器共享文件。所以当我点击我的应用程序中的按钮时,它应该通过我的手机中的谷歌驱动器应用程序上传文件,而无需用户选择应用程序。下面是我迄今为止所做的应用程序的代码。通过其他应用程序共享文件

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button sharingButton = (Button)findViewById(R.id.button1); 

     sharingButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
      shareIt(); 
      } 
      }); 

    } 

    private void shareIt() { 
     //sharing implementation here 
     Intent i=new Intent(android.content.Intent.ACTION_SEND); 
     i.setType("application/pdf"); 
     //i.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject test"); 
     //i.putExtra(android.content.Intent.EXTRA_TEXT, "extra text that you want to put"); 
     Uri uri = Uri.fromFile(getFileStreamPath("/storage/sdcard0/tweek/pdf.png")); 
     i.putExtra(Intent.EXTRA_STREAM,uri); 
     startActivity(Intent.createChooser(i,"Share via")); 
     } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

所以,当我点击按钮它显示我以下错误。

08-02 13:11:40.988: E/AndroidRuntime(25569): FATAL EXCEPTION: main 
08-02 13:11:40.988: E/AndroidRuntime(25569): java.lang.IllegalArgumentException: File /storage/sdcard0/tweek/pdf.png contains a path separator 
08-02 13:11:40.988: E/AndroidRuntime(25569): at android.app.ContextImpl.makeFilename(ContextImpl.java:1921) 
08-02 13:11:40.988: E/AndroidRuntime(25569): at android.app.ContextImpl.getFileStreamPath(ContextImpl.java:945) 
08-02 13:11:40.988: E/AndroidRuntime(25569): at android.content.ContextWrapper.getFileStreamPath(ContextWrapper.java:182) 
08-02 13:11:40.988: E/AndroidRuntime(25569): at com.example.shareapp.MainActivity.shareIt(MainActivity.java:38) 
08-02 13:11:40.988: E/AndroidRuntime(25569): at com.example.shareapp.MainActivity.access$0(MainActivity.java:32) 

有人可以请解释我。我经历了很多文档,但我感到困惑。 在此先感谢。

改变这种

Uri uri = Uri.fromFile(getFileStreamPath("/storage/sdcard0/tweek/pdf.png"));

Uri uri = Uri.fromFile(getFileStreamPath("file:///mnt/storage/sdcard0/tweek/pdf.png")); 

Uri.parse(new File("/mnt/storage/sdcard0/tweek/pdf.png")) 

它应该解决您的崩溃/错误

+0

不,我仍然得到SA m错误。 – Vyshakh

+0

好吧,它向我展示了分享选项,当我选择谷歌驱动器时,它显示的是“上传一个文件”,但在驱动器中没有任何东西。 – Vyshakh

+0

然后检查您的驱动器设置。为什么你使用sdcard0而不是sdcard? – AAnkit