首页 > 解决方案 > 如何共享 Firebase 数据库中的图像?

问题描述

我已经创建了数据库,并且有图像的 Url,我想在我的应用程序中显示该图像正在运行,但现在我想在 Whatsapp 等中共享该图像。现在我需要在那里更改,因为我不知道,怎么做?

这是我的代码:

@Override
public void onBindViewHolder(@NonNull final myViewHolder holder, int position) {


    Picasso.get().load(mdata.get(holder.getAdapterPosition()).getImage()).into(holder.imgView);


    holder.shareBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent;
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), holder.imgView.getId());

            String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) +"/share.png";

          FileOutputStream out = null;

          File file = new File(path);

          try{
              out = new FileOutputStream(file);
              bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
              out.flush();
              out.close();

          }catch(Exception e){
              e.printStackTrace();
          }

          path = file.getPath();

          Uri bmpUri = Uri.parse("file://" + path);

          intent = new Intent(Intent.ACTION_SEND);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          intent.putExtra(Intent.EXTRA_STREAM, bmpUri);
          intent.setType("image/jpeg");
          intent.putExtra(Intent.EXTRA_TEXT," Hello Your's Images ");
          context.startActivity(Intent.createChooser(intent, " Share Images Via"));

标签: androidfirebase

解决方案


您可以使用 uri 执行此操作,并尝试实现下一个代码:

Uri imageUri = Uri.parse(pictureFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//Send to Whattssap
shareIntent.setPackage("com.whatsapp");
//Even you can add text to the image
shareIntent.putExtra(Intent.EXTRA_TEXT, picture_text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

try {
    startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
    ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

就这样


推荐阅读