2016-08-18 48 views
1

在我的应用程序中,我使用的是webview来使用Facebook。在Facebook页面中,我们会发现许多带有图片的帖子。所以要从Facebook下载图片,我使用webpress的longpress方法和HitTestResult方法。我的长时间的工作正在进行,HitTestResult所掌握的数据在那里我很满意。我正确地获取Url(带有https链接的文本)。无法通过在Facebook中使用WebviewHitResults在webview中获取图像URL

我的问题是,如果我longpressed在Facebook图像发布(简单地在图像),我没有得到的图像,而是我得到的URL链接(like-https://something)。如何访问图像并下载它们。

<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" /> 

在我的活动:

权限我在明显增加

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()); 

@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,它没有返回图像,但给出了链接。如果我得到的图像,我可以下载到本地SD卡。

if (hitResult.getType() == WebView.HitTestResult.IMAGE_TYPE || 
        hitResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) 

我试着用Bitmap的代码也得到图像。

 @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; 
      } 
     }); 
    } 

我的应用程序崩溃,如果我使用此代码。

+0

您需要使用该链接并将其转换为位图,然后将其保存在SD卡中。 http://stackoverflow.com/a/11831325/3111083 –

+0

其实它不检查if()条件。我的意思是IMAGE_TYPE和SRC_IMAGE_ANCHOR_TYPE。所以如果我长按图像什么都没有发生。 –

+0

我在调试模式下测试了应用程序,所以我才知道它没有执行if()条件。但hitresult有链接。 –

回答

0

你的问题还不清楚。如果您可以获取图片的网址,那么您可以尝试下载该图片。

但是您确实将下载代码放在点击处理程序中。所以它会在主线程中执行。这将导致在LogCat中清晰可见NetworkOnMainThreadException

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

+0

是的,我能够得到的图像的网址,我不知道进一步的步骤,如果上述代码是不正确的..!你能指导我使用与上述代码相关的Asynk任务吗? –

+0

每天你都可以找到使用AsyncTask从网上下载东西并保存到文件或其他东西的例子。在这里阅读一些'android'标记的页面。或谷歌它。代码中没有什么特别的,它不应该工作。 – greenapps

+0

我试图使用异步任务从webview下载图像,它工作正常。但我在webview中使用Facebook。所以我不得不从脸书上长时间下载图片(如帖子,简介图片等)。当我长时间按下任何图像时,我正在获取该张贴图像页面的链接。如何在longpress上从facebook上下载图片 –