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

问题描述 投票:0回答:1

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

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

在Manifest中我声明了

<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个回答
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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.