在我的应用程序中,我使用的是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;
}
});
}
我的应用程序崩溃,如果我使用此代码。
您需要使用该链接并将其转换为位图,然后将其保存在SD卡中。 http://stackoverflow.com/a/11831325/3111083 –
其实它不检查if()条件。我的意思是IMAGE_TYPE和SRC_IMAGE_ANCHOR_TYPE。所以如果我长按图像什么都没有发生。 –
我在调试模式下测试了应用程序,所以我才知道它没有执行if()条件。但hitresult有链接。 –