私のアプリケーションでは、Facebookを使用するためにwebviewを使用しています。 Facebookページでは、画像付きの投稿が多数見つかります。 Facebookから画像をダウンロードするには、webviewのためにlongpressメソッドとHitTestResultメソッドを使用しています。私の長年のプレスが働いていて、HitTestResultは私が長い間押したデータを持っています。 URL(httpsリンクのテキスト)が正しく取得されています。FacebookでWebviewHitResultsを使用してwebviewで画像URLを取得できない
私の問題は、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;
}
});
}
、私が考える問題がある内の条件であればどこ史上ので、私それはイメージを返すのではなく、URLリンクを提供しています。私は画像を取得する場合、私はそれをローカルのSDカードにダウンロードすることができます。
if (hitResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
hitResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE)
イメージを取得するためにビットマップでコードを試しました。
@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()条件を実行していないことを知りました。しかし、ヒットレスはURLリンクを持っています。 –