代码之家  ›  专栏  ›  技术社区  ›  CantThinkOfAnything

将浏览器页面共享到我的应用程序并获取屏幕截图

  •  0
  • CantThinkOfAnything  · 技术社区  · 9 年前

    我很难从意向中获取一些信息。例如,当用户使用谷歌chrome浏览互联网,然后单击菜单按钮并按下共享时,就会产生这种意图。之后会出现一个能够过滤意图的应用程序列表。

    收到意图后,我已转储内容,这是输出:

    07-18 12:03:13.825  24548-24548/com.example.expandtest I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@423bdbc0 time:41381656
    07-18 12:03:30.935  24548-24548/com.example.expandtest E/Activity﹕ Dumping Intent start
    07-18 12:03:30.935  24548-24548/com.example.expandtest E/Activity﹕ [android.intent.extra.SUBJECT=dick swaab - Google Search]
    07-18 12:03:30.935  24548-24548/com.example.expandtest E/Activity﹕ [android.intent.extra.TEXT=https://www.google.nl/search?q=dick+swaab&oq=dick+swaab&aqs=chrome..69i57.5033j0j4&client=ms-android-huawei&sourceid=chrome-mobile&espv=1&ie=UTF-8]
    07-18 12:03:30.945  24548-24548/com.example.expandtest E/Activity﹕ [share_screenshot_as_stream=content://com.android.chrome.FileProvider/images/screenshot/1437213810717-617998691.jpg]
    07-18 12:03:30.945  24548-24548/com.example.expandtest E/Activity﹕ Dumping Intent end
    

    所以看一下内容,很明显,当你在浏览器中使用共享按钮时,会拍摄一张屏幕截图。看到这里,我也想提取截图,而不仅仅是URL。

    但是,当我尝试提取图像的Uri时,它给了我空值。准确地说,这行代码:

    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    

    尽管意图的转储内容清楚地显示有可用的屏幕截图,但我无法提取它。转储还显示图像URI是一个流,所以我想我应该能够用上面的行提取它,但我做了一些错误。

    因此,我尝试了不同的方法,但唯一可行的方法是以与输出中转储内容相同的方式提取URI部分,这意味着手动提取字符串的URI部分。但如果我能用android的方式做这件事,我想它看起来会更好。

    我所尝试的:

    创建时:

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent); // Handle text being sent
        }
        if (type.startsWith("image/")) {
            handleSendImage(intent); // Handle single image being sent
        }
    }
    
    dumpIntent(intent);
    

    获取数据的方法:

    void handleSendText(Intent intent) {
        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (sharedText != null) {
            // Update UI to reflect text being shared
            Log.d(LOG_TAG, "handle text");
            //String uRL = intent.getStringExtra(Intent.EXTRA_TEXT);
            mArticleURL.setText(sharedText);
    
           // Uri imageUri = null;
    
    
            // i tried using the handleSendImage code here to see if it would work
            try {
                //imageUri = (Uri) intent.getExtras().get(Intent.EXTRA_STREAM);
    
                // imageUri is always null
                Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
                if (imageUri != null) {
                    //Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
                    Log.d(LOG_TAG, "image exists");
    
                    mWebThumbNail.setImageURI(null);
                    mWebThumbNail.setImageURI(imageUri);
                }
            } catch (Throwable t) {
                Log.d(LOG_TAG, "no image available" + t.getMessage());
            }
    
        }
    }
    
    void handleSendImage(Intent intent) {
        Log.d(LOG_TAG, "handle image");
    
        Uri imageUri = (Uri) intent.getExtras().get(Intent.EXTRA_STREAM);
        if (imageUri != null) {
            //Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
            Log.d(LOG_TAG, "image exists");
    
            mWebThumbNail.setImageURI(null);
            mWebThumbNail.setImageURI(imageUri);
        }
    }
    
    public static void dumpIntent(Intent i) {
    
        Bundle bundle = i.getExtras();
        if (bundle != null) {
            Set<String> keys = bundle.keySet();
            Iterator<String> it = keys.iterator();
            Log.e(LOG_TAG, "Dumping Intent start");
            while (it.hasNext()) {
                String key = it.next();
                Log.e(LOG_TAG, "[" + key + "=" + bundle.get(key) + "]");
            }
            Log.e(LOG_TAG, "Dumping Intent end");
        }
    }
    

    如果有人能指出我的缺点,我会很高兴的!

    为了完整起见,这是我的清单

    <activity
        android:name=".ShareLink"
        android:label="Add to LinkHub" >
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>
    

    问题不应该出现在清单上,因为我已经在谷歌上搜索了很多。我总是可能错过了什么。

    1 回复  |  直到 9 年前
        1
  •  4
  •   greenapps    9 年前
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);.
    

    你试过了吗

    Uri imageUri = (Uri) intent.getParcelableExtra( "share_screenshot_as_stream"); 
    

    相反