【问题标题】:how to get data from intent in the form of uri?如何以uri的形式从意图中获取数据?
【发布时间】:2016-10-26 18:37:24
【问题描述】:

我有 uri 类型的数据,我使用 putExtra() 将数据放入意图中,但我不知道如何从 uri 中获取数据。

else if(requestCode==2){

           if (data != null) {
                   Uri uri = data.getData();

                   Intent intent = new Intent(getBaseContext(),BackUp_Main.class);
                   intent.putExtra("singleImage", uri);
                   startActivity(intent);

               }
}

如何从 uri 中获取 uri 数据?

【问题讨论】:

标签: android


【解决方案1】:

保存时

String str = myUri.toString();
intent.putExtra("singleImage", uri);

加载时

String str = getIntent().getStringExtra("singleImage");
Uri myUri = Uri.parse(str);

希望对你有帮助:)

【讨论】:

  • 将 uri 更改为 str 因为 myUri.toString();存储在 str
【解决方案2】:

您可以将数据保存为字符串并从字符串中解析 uri

【讨论】:

    【解决方案3】:

    试试

    intent.putExtra("singleImage", uri.toString());
    

    【讨论】:

      【解决方案4】:

      您可以按如下方式将 uri 转换为字符串:-

      intent.putExtra("singleImage", uri.toString());
      

      然后在获取意图的同时将 String 转换为 uri 回来

      Uri myUri = Uri.parse(extras.getString("singleImage"));
      

      【讨论】:

        【解决方案5】:

        你可以像这样解析并获取Uri

        // Add an Uri instance to an Intent
        intent.putExtra("imageUri", uri);
        

        然后,当你需要它的时候,就这样拿回来:

        // Get an Uri from an Intent
        Uri uri = intent.getParcelableExtra("imageUri");
        

        【讨论】:

          猜你喜欢
          • 2016-09-06
          • 2021-04-11
          • 2020-11-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多