【问题标题】:sharing file through other app通过其他应用共享文件
【发布时间】:2016-01-07 03:06:52
【问题描述】:

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

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)

谁能解释一下。我浏览了很多文档,但我感到困惑。 提前致谢。

【问题讨论】:

    标签: android android-intent file-sharing


    【解决方案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"))
    

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

    【讨论】:

    • 好的,它显示了共享选项,当我选择谷歌驱动器时,它显示吐司为“上传一个文件”,但驱动器中什么都没有。
    • 然后检查您的驱动器设置。为什么你使用 sdcard0 而不是 sdcard?
    • 那是我手机里的SD卡。外部 sd 卡是 extSdcard。当我尝试通过电子邮件或 gmail 发送它时,它显示该文件不存在。但文件在那里。
    • Uri.parse(new File(/mnt/storage/sdcard0/tweek/pdf.png")) 解决了我的问题。谢谢。
    • @Michal YW,快乐编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2012-05-09
    • 2021-10-28
    相关资源
    最近更新 更多