我想从手机存储的WebView应用中下载图片,并将该图片分享给其他社交应用......我已经创建了一个下载管理器请求,并设置了目标路径,还创建了一个共享图片的广播接收器。
但我收到了接收广播意图的错误,并且当我给出目的地URI时,应用程序已经崩溃了,但当我没有提到目的地URI时,图像将在缓存中下载并分享。但是我想把图片保存在手机存储空间本地存储,而不是SD卡中,所以请大家帮忙做一下。
这是我的广播接收器的代码。
final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
DownloadManager.Query req_query = new DownloadManager.Query();
req_query.setFilterById(queueid);
Cursor c = dm.query(req_query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uris = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
// webView.loadUrl(uris);
// Toast.makeText(getApplicationContext(), uris, Toast.LENGTH_LONG).show();
String currUrl = webView.getUrl();
String line = "Go to the link..";
String text = line+"\n\n"+currUrl;
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(uris));
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}
else {
Toast.makeText(getApplicationContext(), "Not Download", Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(getApplicationContext(), "Not Download", Toast.LENGTH_LONG).show();
}
}
}
};
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
这是我的下载管理器请求的代码。
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// If url contains mailto link then open Mail Intent
if (url.contains("mailto:")) {
// Could be cleverer and use a regex
//Open links in new browser
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// Here we can open new activity
return true;
} else {
if(url.contains("whatsapp")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
} catch (Exception e) {
Log.i(TAG, "shouldOverrideUrlLoading Exception:" + e);
return true;
}
}
else {
if(url.contains("jpg") || url.contains("jpeg") || url.contains("png") || url.contains("JPG") || url.contains("JPEG") || url.contains("PNG")) {
dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Uri path = Uri.parse("file://mnt/sdcard/App/Images/"+url);
request.setDestinationUri(path);
queueid = dm.enqueue(request);
}
else {
// Stay within this webview and load url
view.loadUrl(url);
return true;
}
}
}
return true;
}