我想从手机存储中的WebView应用下载图像并将该图像共享给其他社交应用... 我创建了一个下载管理器请求并设置了目标的路径,还创建了一个共享图像的广播接收器。
但是我收到广播意图时出错,当我给destinationUri时,应用程序崩溃了,但是当我不提及目标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;
}