【发布时间】:2016-08-18 06:57:43
【问题描述】:
在我的应用程序中,我使用 webview 来使用 Facebook。在 Facebook 页面中,我们会发现许多带有图片的帖子。所以要从 Facebook 下载图像,我使用的是 webview 的 longpress 方法和 HitTestResult 方法。我的长按正在工作,HitTestResult 正在获得我长按的数据。我正在正确获取 URL(带有 https 链接的文本)。
我的问题是,如果我在 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,它不是返回图像而是提供 url 链接。如果我得到图像,我可以将其下载到本地 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 卡中。 stackoverflow.com/a/11831325/3111083
-
其实它并没有检查 if() 条件。我的意思是 IMAGE_TYPE 和 SRC_IMAGE_ANCHOR_TYPE。所以如果我长按图像什么都不会发生。
-
我在调试模式下测试了应用程序,所以我知道它没有执行 if() 条件。但命中结果有 url 链接。
-
当然会崩溃,因为你是在 MainThread 上做的。 stackoverflow.com/questions/9671546/asynctask-android-example 这应该可以帮助您了解异步任务。
标签: java android facebook image webview