在我的应用程序中,我使用webview来使用Facebook。在Facebook页面上,我们会发现许多带有图片的帖子。因此,为了从Facebook下载图片,我使用了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;
}
});
}如果我使用这个代码,我的应用程序就会崩溃。
发布于 2016-08-18 15:42:49
你的问题还不清楚。如果你能得到图片的url,那么你就可以尝试下载该图片。
但您确实将下载代码放在了一个单击处理程序中。所以它将在主线程上执行。这将使LogCat中的NetworkOnMainThreadException清晰可见。
为了防止这种情况,您应该将下载代码放在线程或AsyncTask中。
https://stackoverflow.com/questions/39011741
复制相似问题