【问题标题】:How to send a file from my app to another many application which do support sharing data如何将文件从我的应用程序发送到另一个支持共享数据的应用程序
【发布时间】:2019-05-31 06:22:36
【问题描述】:

我实际上是在尝试将来自 API 的数据(作为文本而不是文件)发送给我,并且我想通过许多应用程序将这些数据作为文本文件发送,其中包括像 WhatsApp 这样的文件共享、远足、电报、分享等。

【问题讨论】:

    标签: android android-sharing


    【解决方案1】:

    -你可以试试这个代码,看看它是否对你有用:

    Intent shareIntent = new Intent("android.intent.action.SEND");
    shareIntent.setType("text/plain");               
    shareIntent.putExtra("android.intent.extra.SUBJECT",
                         "Codes Easy");              
    shareIntent.putExtra("android.intent.extra.SUBJECT",
             "Add a text to be shared here");
    startActivity(Intent.createChooser(shareIntent, "Share with"));
    

    【讨论】:

    • 我已经尝试过了,它只能以纯文本形式发送文本,它不会创建扩展名为 .txt 的文件,我想使用意图发送该 .txt 文件,你能建议一个 MIME .txt?
    【解决方案2】:
    <data android:scheme="file" /> //Means local file
    <data android:mimeType="*/*"/> //This accept any mimeType
    <data android:pathPattern=".*\\.txt" /> //Your excepted extention
    

    试试这个代码:

    private Uri getUriForFile() {
      Intent intent = getIntent();
      String action = intent.getAction();
      String type = intent.getType();
      if (TextUtils.equals(Intent.ACTION_SEND, action) 
               && !TextUtils.isEmpty(type)) {
         Uri uri = 
             intent.getParcelableExtra(Intent.EXTRA_STREAM);
         if (uri != null) {
            Log.e("uri",uri.toString());
            return  uri;
         }
      }
      return null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2017-01-30
      相关资源
      最近更新 更多