如何在 android 中使用其他共享选项添加自定义选项(下载图片)
How to add a custom option(Dowload Image) with other share options in android
是否可以在意图选择器中添加自定义选项(例如下载图片)和其他共享选项(如附图所示)?
参考这个答案:Link
示例代码:
Github Project
很遗憾,我没有得到有用的答案。我做了一个解决方法来解决这个问题。
这是添加自定义“保存到图库”选项的代码(如附图所示)
Intent downloadIntent = new Intent(aContext, DownloadActivity.class);
downloadIntent.putExtra("url", url);
intentList.add(new LabeledIntent(downloadIntent, "com.don.offers", "Save To Gallery", R.drawable.ic_save_to_gallery));
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
这里的DownloadActivity是透明的activity,里面有下载图片的代码。
public class DownloadActivity extends AppCompatActivity {
String imageUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
imageUrl = getIntent().getStringExtra("url");
saveToGallery();
finish();
}
public void saveToGallery() {
try {
String DIR_NAME = "Don Downloaded Images";
String filename = "DON_IMG_" + System.currentTimeMillis();
//String downloadUrlOfImage = "http://fun.localdon.com/DonUGCImg/DON_memes14908366211071492831321991.png";
String downloadUrlOfImage = imageUrl;
File direct =
new File(Environment
.getExternalStoragePublicDirectory(String.valueOf(Environment.DIRECTORY_PICTURES))
.getAbsolutePath() + "/" + DIR_NAME + "/");
if (!direct.exists()) {
direct.mkdir();
Log.e("saveToGallery", "dir created for first time");
}
DownloadManager dm = (DownloadManager) DownloadActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(downloadUrlOfImage);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(filename)
.setMimeType("image/jpeg")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(String.valueOf(Environment.DIRECTORY_PICTURES),
File.separator + DIR_NAME + File.separator + filename);
dm.enqueue(request);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
final Uri contentUri = Uri.fromFile(direct);
scanIntent.setData(contentUri);
DownloadActivity.this.sendBroadcast(scanIntent);
} else {
final Intent intent1 = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
DownloadActivity.this.sendBroadcast(intent1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果谁有更好的方法请share.Thanks
是否可以在意图选择器中添加自定义选项(例如下载图片)和其他共享选项(如附图所示)?
参考这个答案:Link
示例代码:
Github Project
很遗憾,我没有得到有用的答案。我做了一个解决方法来解决这个问题。
这是添加自定义“保存到图库”选项的代码(如附图所示)
Intent downloadIntent = new Intent(aContext, DownloadActivity.class);
downloadIntent.putExtra("url", url);
intentList.add(new LabeledIntent(downloadIntent, "com.don.offers", "Save To Gallery", R.drawable.ic_save_to_gallery));
// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
这里的DownloadActivity是透明的activity,里面有下载图片的代码。
public class DownloadActivity extends AppCompatActivity {
String imageUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
imageUrl = getIntent().getStringExtra("url");
saveToGallery();
finish();
}
public void saveToGallery() {
try {
String DIR_NAME = "Don Downloaded Images";
String filename = "DON_IMG_" + System.currentTimeMillis();
//String downloadUrlOfImage = "http://fun.localdon.com/DonUGCImg/DON_memes14908366211071492831321991.png";
String downloadUrlOfImage = imageUrl;
File direct =
new File(Environment
.getExternalStoragePublicDirectory(String.valueOf(Environment.DIRECTORY_PICTURES))
.getAbsolutePath() + "/" + DIR_NAME + "/");
if (!direct.exists()) {
direct.mkdir();
Log.e("saveToGallery", "dir created for first time");
}
DownloadManager dm = (DownloadManager) DownloadActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(downloadUrlOfImage);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(filename)
.setMimeType("image/jpeg")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(String.valueOf(Environment.DIRECTORY_PICTURES),
File.separator + DIR_NAME + File.separator + filename);
dm.enqueue(request);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
final Uri contentUri = Uri.fromFile(direct);
scanIntent.setData(contentUri);
DownloadActivity.this.sendBroadcast(scanIntent);
} else {
final Intent intent1 = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
DownloadActivity.this.sendBroadcast(intent1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果谁有更好的方法请share.Thanks