【问题标题】:How to share image through inten如何通过意图共享图像
【发布时间】:2018-10-15 04:20:31
【问题描述】:
 Bundle intent = getIntent().getExtras();`
 cardView=(CardView)findViewById(R.id.card);
 final String query = intent.getString("Query1");

 db = new DataBaseHelper(Image.this);    
 Cursor c = db.getData(query);
   if (c.getCount() != 0) {
       c.moveToFirst();
       do {
           image = c.getString(5);
           title=c.getString(3);
        } while (c.moveToNext());
    }   
    txt.setText(title);
    img.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(image, "drawable", getPackageName())));

【问题讨论】:

    标签: java android


    【解决方案1】:

    要为图像创建共享意图,您可以使用以下代码。

    uriToImage 是您的图像文件的Uri

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
    shareIntent.setType("image/jpeg");
    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
    

    编辑 - 获取可绘制图像的 URI

    Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
    

    【讨论】:

    • 如何为 imageview 获取 uriToImage
    • 查看这篇文章以获得答案stackoverflow.com/questions/32558021/…
    • 我正在使用 sqlite 数据库图像存储字符串名称,并且 iam 将图像获取到“可绘制”。iam 在 inage 视图中列出图像,但无法通过我的代码意图共享图像:
    • img.setImageDrawable(getResources().getDrawable(getResources().getIdentifier(image, "drawable", getPackageName())));
    • 这个img视图点击通过意图分享意图
    【解决方案2】:

    使用 Content Provider(FileProvider) 共享数据是一种很好的做法。

      Intent intent = new Intent(Intent.ACTION_SEND);
      final String path = "Your File Path";
      Uri uri = FileProvider.getUriForFile(YourActivity.this, "com.abc.xy.appname.fileprovider", new File(path));
      intent.setDataAndType(uri, "image/*");
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
       intent.putExtra(Intent.EXTRA_STREAM, uri);
       startActivity(intent);
    

    用于多个文件共享

    ArrayList<Uri> files = new ArrayList<Uri>();
                for (int i = 0; i < mfileList.size(); i++) {
                    File file = new File(mfileList.get(i).get_path());
                    Uri uri = FileProvider.getUriForFile(YourActivity.this, "com.abc.xy.appname.fileprovider", file);
                    files.add(uri);
                }
    
                Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                intent.setType("image/jpeg");
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra(Intent.EXTRA_STREAM, files);
                    startActivity(intent);
    

    分享存储在 Drawable 中的图像。将此 Uri 放入 Share Intent Extras 中。

    Uri uri = Uri.parse("android.resource://your.package/drawable/image_name");
    

    查看此链接以了解内容提供程序设置。

    [https://developer.android.com/reference/android/support/v4/content/FileProvider][1]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2012-05-05
      相关资源
      最近更新 更多