我正在尝试在我的Android应用程序中显示的卡片上提供共享功能。我已经了解了ShareactionProvider。这个actionProvider假设出现在每个cardview上。我在适配器的onBindViewHolder上放置了ShareActionprovider的代码。当应用程序启动时,所有卡片同时执行其共享。我只想在用户请求时调用此共享。
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final Cursor cursor) {
final NewsFacade facade = NewsFacade.fromCursor(cursor);
MenuItem shareMenuItem = viewHolder.toolbar.getMenu().findItem(R.id.share);
ShareActionProvider shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareMenuItem);
shareActionProvider.setShareIntent(viewHolder.sendShareIntent(facade, context));
shareActionProvider.setShareHistoryFileName("custom_share_history.xml");
以下是帮助分享的助手方法:
public Intent sendShareIntent(NewsFacade facade, Context context)
{
saveImageToCache(facade, context);
return shareImage(context);
}
private void saveImageToCache(NewsFacade facade, Context context)
{
try {
Bitmap bitmap = facade.getThumb();
File cachePath = new File(context.getCacheDir(), "images");
cachePath.mkdirs();
FileOutputStream stream = new FileOutputStream(cachePath + "/image.png"); // overwrites this image every time
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private Intent shareImage(Context context)
{
File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.clinton.companion.fileprovider", newFile);
if (contentUri != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(contentUri,context.getContentResolver().getType(contentUri));
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
context.startActivity(Intent.createChooser(shareIntent, "Choose an app"));
return shareIntent;
}
return null;
}
我知道,我在错误的地方调用ActionProvider。但是在其他地方,我应该调用它,我需要cardview持有的对象的实例,所以我可以在cardview中获取图像以进行共享。
请帮助任何帮助。
答案 0 :(得分:0)
我修好了。 我在打电话
explode(PHP_EOL, $string, -(count(explode(PHP_EOL, $string )) - 5 ));
自动执行共享。