【问题标题】:Android get data from gmail, attachment is jsonAndroid从gmail获取数据,附件是json
【发布时间】:2019-03-28 21:43:46
【问题描述】:

我想要什么:从 gmail 附件中的 json 获取数据。

说明: 打开Gmail,点击附件是json文件。我需要处理的 Json 文件句柄数据。

在我声明的清单中

<intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <data android:mimeType="application/json"/>
                <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

接下来我用

Intent intent = getIntent();        
if(intent.getAction().equals(Intent.ACTION_VIEW)){ 
intent.getData <- I suppose from here i can get my infomation
        }

但是 intent.getData 只返回 Uri,而且我读到有人我必须使用 ContentResolver、InputStream 和其他东西来例如将我的数据从 json 写入字符串。如果有人可以通过示例解释它的工作原理,我将不胜感激。

【问题讨论】:

    标签: android json android-intent gmail attachment


    【解决方案1】:

    好的,我知道了

    if (intent.getAction().equals(Intent.ACTION_VIEW)) {
                Toast.makeText(this, "work", Toast.LENGTH_SHORT).show();
                Uri data = intent.getData();
    //            ContentResolver contentResolver = getContentResolver();
                String text = getStringFromShare(data);
                Log.d("sasas", "onCreate: sometext");
            }
        }
    
        private String getStringFromShare(Uri data) {
            String text = null;
            try {
    
                ContentResolver cr = getApplicationContext().getContentResolver();
                InputStream is = cr.openInputStream(data);
                if (is != null) {
    
                    StringBuffer buf = new StringBuffer();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    String str;
                    if (is != null) {
                        while ((str = reader.readLine()) != null) {
                            buf.append(str);
                        }
                    }
                    is.close();
                    text = buf.toString();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return text;
        }
    

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      相关资源
      最近更新 更多