首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用Facebook中的WebviewHitResults抓取webview中的图像URL

无法使用Facebook中的WebviewHitResults抓取webview中的图像URL
EN

Stack Overflow用户
提问于 2016-08-18 14:57:43
回答 1查看 534关注 0票数 1

在我的应用程序中,我使用webview来使用Facebook。在Facebook页面上,我们会发现许多带有图片的帖子。因此,为了从Facebook下载图片,我使用了longpress方法和HitTestResult方法来查看网页。我的长时间按键正在工作,而HitTestResult正在拥有我长按的数据。我得到的Url的(文本与https链接)正确。

我的问题是,如果我长按facebook的图片发布(简单的图片),我得到的不是图片,而是url链接(like-https://something)。如何访问图像并下载它们。

我在清单中添加的权限:

代码语言:javascript
复制
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

在我的活动中:

代码语言:javascript
复制
WebSettings settings = ((WebView) findViewById(R.id.webview)).getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccess(true);
settings.setAppCacheEnabled(true);
settings.setLoadWithOverviewMode(true);
webView2.setWebViewClient(new PQClient());
webView2.setWebChromeClient(new PQChromeClient());

代码语言:javascript
复制
@Override
protected void onResume() {
    super.onResume();
    final WebView wv = (WebView) findViewById(R.id.webview);
    wv.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                WebView.HitTestResult hitResult = null;
                hitResult = wv.getHitTestResult();
                if (hitResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
                        hitResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                    try {
                        String imageUrl = hitResult.getExtra();
                        Toast.makeText(SocialMediaActivity.this,imageUrl,Toast.LENGTH_LONG).show();
                        URL url = new URL(imageUrl);
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        urlConnection.setRequestMethod("GET");
                        urlConnection.setDoOutput(true);
                        urlConnection.connect();

                        File sdcard = Environment.getExternalStorageDirectory();
                        File file = new File(sdcard, "filename.ext");

                        FileOutputStream fileOutput = new FileOutputStream(file);
                        InputStream inputStream = urlConnection.getInputStream();

                        byte[] buffer = new byte[1024];
                        int bufferLength = 0;

                        while ((bufferLength = inputStream.read(buffer)) > 0) {
                            fileOutput.write(buffer, 0, bufferLength);
                        }
                        fileOutput.close();

                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                return true;
            }
        });
    }

在这里,我认为问题出在if条件下,因为无论我在哪里执行longpress,它都不是返回图像,而是给出url链接。如果我得到图像,我可以下载到本地SD卡。

代码语言:javascript
复制
if (hitResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
                    hitResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE)

我也尝试了使用Bitmap的代码来获取图像。

代码语言:javascript
复制
     @Override
protected void onResume() {
    super.onResume();
    final WebView wv = (WebView) findViewById(R.id.webview);
    wv.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                WebView.HitTestResult hitResult = null;
                hitResult = wv.getHitTestResult();
                    try {
                        String imageUrl = hitResult.getExtra();
                        URL url = new URL(imageUrl);
                        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                        Toast.makeText(SocialMediaActivity.this,imageUrl,Toast.LENGTH_LONG).show();
                        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                        image.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

                    //you can create a new file name "test.jpg" in sdcard folder.
                        File f = new File(Environment.getExternalStorageDirectory()
                                + File.separator + "test.jpg");
                        f.createNewFile();
                    //write the bytes in file
                        FileOutputStream fo = new FileOutputStream(f);
                        fo.write(bytes.toByteArray());

                    // remember close de FileOutput
                        fo.close();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                return true;
            }
        });
    }

如果我使用这个代码,我的应用程序就会崩溃。

EN

回答 1

Stack Overflow用户

发布于 2016-08-18 15:42:49

你的问题还不清楚。如果你能得到图片的url,那么你就可以尝试下载该图片。

但您确实将下载代码放在了一个单击处理程序中。所以它将在主线程上执行。这将使LogCat中的NetworkOnMainThreadException清晰可见。

为了防止这种情况,您应该将下载代码放在线程或AsyncTask中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39011741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档