webview.gethittestresult();在facebook和instagram中不返回图像

j0pj023g  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(223)

我想从facebook和instagram下载一张图片,但是 webView.getHitTestResult(); facebook和instagram没有任何回报,但我可以从谷歌等其他网站下载图片。下面是我使用的代码:
instagramactivity.java文件

public class InstagramActivity extends AppCompatActivity {

    WebView webView;
    String HTTP_URL = "https://www.google.com" ;
    InstagramActivity activity;
    String picUrl = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_instagram);

        webView = (WebView)findViewById(R.id.WebView1);
        activity = this;

        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        registerForContextMenu(webView);
        webView.loadUrl(HTTP_URL);

        webView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                WebView.HitTestResult hitTestResult = webView.getHitTestResult();
                // If it's an image type or a type with an image link
                if (hitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
                        hitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

                    Toast.makeText(InstagramActivity.this, "image", Toast.LENGTH_SHORT).show();

                    // pop up a dialog to save the image
                    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                    builder.setTitle("prompt");
                    builder.setMessage("Save image to local");
                    builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            picUrl = hitTestResult.getExtra();//Get image link
                            //Save image to album
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    url2bitmap(picUrl);
                                }
                            }).start();
                        }
                    });
                    builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                        // automatic dismiss
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                    return true;
                }

                else
                    Toast.makeText(InstagramActivity.this, "Not Image", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    public void url2bitmap(String url) {
        Bitmap bm = null;
        try {
            URL iconUrl = new URL(url);
            URLConnection conn = iconUrl.openConnection();
            HttpURLConnection http = (HttpURLConnection) conn;
            int length = http.getContentLength();
            conn.connect();
            // Get the character stream of the image
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, length);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
            if (bm != null) {
                save2Album(bm);
            }
        } catch (Exception e) {
            new Handler().post (new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(activity, "Save failed", Toast.LENGTH_SHORT).show();
                }
            });
            e.printStackTrace();
        }
    }

    private void save2Album(Bitmap bitmap) {
        File appDir = new File(Environment.getExternalStorageDirectory(), "QR code####");
        if (!appDir.exists()) appDir.mkdir();
        String[] str = picUrl.split("/");
        String fileName = str[str.length - 1];
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            onSaveSuccess(file);
        } catch (IOException e) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(activity, "Save failed", Toast.LENGTH_SHORT).show();
                }
            });
            e.printStackTrace();
        }
    }

    private void onSaveSuccess(final File file) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                activity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
                Toast.makeText(activity, "successfully saved to: album - QR code####", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

活动\ instagram.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.InstagramActivity">

    <WebView
        android:id="@+id/WebView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

</RelativeLayout>

正如我已经提到的,除了facebook/instagram之外,这段代码在其他一些网站上运行良好。谢谢您

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题