通过其他应用共享文件

时间:2013-08-02 08:01:54

标签: android android-intent file-sharing

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

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)

有人可以解释一下。我经历了很多文档,但我感到很困惑。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

更改此

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"))

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