Android从gmail获取数据,附件为json

时间:2019-03-28 21:43:46

标签: android json android-intent gmail attachment

我想要的内容:从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写入字符串。如果有人可以解释它在示例中的工作原理,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

好吧,我拥有

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;
    }
相关问题