如何通过inten共享图像

时间:2018-10-10 10:51:21

标签: java android

 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())));

2 个答案:

答案 0 :(得分:1)

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

Traceback (most recent call last): File "pytest8.py", line 23, in <module> cmds1.kill() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1564, in kill self.send_signal(signal.SIGKILL) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1554, in send_signal os.kill(self.pid, sig) OSError: [Errno 3] No such process 是图像文件的uriToImage

Uri
  

编辑-获取可绘制图像的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)));

答案 1 :(得分:0)

按照惯例,使用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]