【问题标题】:Share text and image with android intent使用 android 意图共享文本和图像
【发布时间】:2017-03-22 19:57:36
【问题描述】:

这是我的java代码

  intent.putExtra(Intent.EXTRA_SUBJECT, "My App name and some text");
    intent.putExtra(Intent.EXTRA_TEXT, "a link");
    intent.putExtra(Intent.EXTRA_STREAM,getImageUri(context,mBitmap));
    intent.setType("image/*,text/plain");
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

我想分享图片和文字。此代码适用于 WhatsApp、Twitter、Gmail 等 .. 但不适用于 Facebook

提前感谢您的帮助

【问题讨论】:

  • 我假设您使用的是ACTION_SEND,在这种情况下有两个明显的问题。首先,setType() 不采用逗号分隔的列表。其次,ACTION_SEND 支持 either EXTRA_TEXT or EXTRA_STREAM,不能同时支持两者。 the ACTION_SEND documentation 涵盖了这两点。给定您的代码,将其更改为 intent.setType("image/*"); 看看是否有帮助。如果没有,请编辑您的问题以解释“在 Facebook 上不起作用”的含义并发布 getImageUri() 正在返回的内容。
  • 因为 android 4.3 setType() 可以采用逗号分隔的列表,并且 ACTION_SEND 支持 EXTRA_TEXT 和 EXTRA_STREAM。我说我的代码适用于 twitter、whatsapp 和 gmail
  • “因为 android 4.3 setType() 可以采用逗号分隔的列表”——即not documented。 “ACTION_SEND 支持 EXTRA_TEXT 和 EXTRA_STREAM”——不能同时支持。引用the documentation,“get*Extra 可以有 either EXTRA_TEXT EXTRA_STREAM 字段,包含要发送的数据。如果使用 EXTRA_TEXT,MIME 类型应为“text /plain";否则应该是EXTRA_STREAM中数据的MIME类型"
  • 应用程序可以选择尝试同时支持EXTRA_TEXTEXTRA_STREAM。但他们没有必须,因为文档说他们不必这样做。不要期望所有应用都支持它们。

标签: android facebook android-intent


【解决方案1】:

发送文本内容

ACTION_SEND 操作最直接和最常见的用法是将文本内容从一个活动发送到另一个活动。例如,内置的浏览器应用程序可以将当前显示页面的 URL 作为文本共享给任何应用程序。这对于通过电子邮件或社交网络与朋友分享文章或网站很有用。下面是实现这种共享的代码:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

发送二进制内容

使用 ACTION_SEND 操作与设置适当的 MIME 类型并将数据的 URI 放置在名为 EXTRA_STREAM 的额外数据中来共享二进制数据。这通常用于共享图像,但可用于共享任何类型的二进制内容:

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

发送多条内容

要共享多条内容,请使用 ACTION_SEND_MULTIPLE 操作以及指向内容的 URI 列表。 MIME 类型根据您共享的内容组合而有所不同。例如,如果您共享 3 张 JPEG 图片,则类型仍为“image/jpeg”。对于图像类型的混合,它应该是 "image/" 以匹配处理任何类型图像的活动。如果您要共享多种类型,则应仅使用“/*”。如前所述,由接收应用程序来解析和处理您的数据。这是一个例子:

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.add(imageUri2);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));

For More Info Please Visit Official Document

【讨论】:

    【解决方案2】:

    如果您想在 Facebook 上共享图像和文本而不使用 Facebook SDK,那么您必须创建图像和文本的位图,然后您可以在 facebook 上共享该位图。

    从这里下载源代码 (Share image and text on facebook using intent in android)

    activity_main.xml

    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <EditText
            android:id="@+id/et_text"
            android:layout_width="match_parent"
            android:textSize="15dp"
            android:layout_height="45dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/edittext_drawable"
            android:hint="Enter your text"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:paddingRight="10dp"
            android:inputType="text"
            android:imeOptions="actionDone"
            android:paddingLeft="10dp"
            android:singleLine="true"
            android:textColorHint="#979797" />
    
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/rl_main"
            android:background="#ffffff"
            android:layout_below="@+id/et_text"
            android:layout_above="@+id/tv_share">
    
    
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:src="@drawable/index"
                android:scaleType="fitXY"
                android:id="@+id/iv_image"
                android:layout_marginTop="10dp"
                />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp"
                android:id="@+id/tv_text"
                android:layout_below="@+id/iv_image"
                android:layout_margin="10dp"
                android:textColor="#000000"
                android:maxLines="5"
                />
    
        </RelativeLayout>
    
    
    
        <TextView
            android:id="@+id/tv_share"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#F38D0A"
            android:gravity="center"
            android:padding="10dp"
            android:layout_margin="10dp"
            android:text="Share"
            android:textColor="#ffffff"
            android:textSize="15dp"
            android:layout_alignParentBottom="true"/>
    
        </RelativeLayout>
    

    MainActivity.java

    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        EditText et_text;
        ImageView iv_image;
        TextView tv_share,tv_text;
        RelativeLayout rl_main;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            init();
    
        }
    
        private void init(){
            et_text = (EditText)findViewById(R.id.et_text);
            iv_image = (ImageView)findViewById(R.id.iv_image);
            tv_share = (TextView)findViewById(R.id.tv_share);
            rl_main = (RelativeLayout)findViewById(R.id.rl_main);
            tv_text= (TextView) findViewById(R.id.tv_text);
    
            File dir = new File("/sdcard/Testing/");
            try {
                if (dir.mkdir()) {
                    System.out.println("Directory created");
                } else {
                    System.out.println("Directory is not created");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            tv_share.setOnClickListener(this);
    
            et_text.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    tv_text.setText(et_text.getText().toString());
    
                }
            });
    
    
        }
    
    
    
    
        @Override
        public void onClick(View v) {
    
            switch (v.getId()){
                case R.id.tv_share:
                    Bitmap bitmap1 = loadBitmapFromView(rl_main, rl_main.getWidth(), rl_main.getHeight());
                    saveBitmap(bitmap1);
                    String str_screenshot = "/sdcard/Testing/"+"testing" + ".jpg";
    
                    fn_share(str_screenshot);
                    break;
            }
    
        }
    
        public void saveBitmap(Bitmap bitmap) {
            File imagePath = new File("/sdcard/Testing/"+"testing" + ".jpg");
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(imagePath);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.flush();
                fos.close();
    
                Log.e("ImageSave", "Saveimage");
            } catch (FileNotFoundException e) {
                Log.e("GREC", e.getMessage(), e);
            } catch (IOException e) {
                Log.e("GREC", e.getMessage(), e);
            }
        }
    
        public static Bitmap loadBitmapFromView(View v, int width, int height) {
            Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);
            v.draw(c);
    
            return b;
        }
    
        public void fn_share(String path) {
    
            File file = new File("/mnt/" + path);
    
            Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());
            Uri uri = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("image/*");
            intent.putExtra(Intent.EXTRA_STREAM, uri);
    
            startActivity(Intent.createChooser(intent, "Share Image"));
    
    
        }
    }
    

    谢谢!

    【讨论】:

      【解决方案3】:

      您可以将图像的 int 值共享为 R.drawable.image 并使用 getResources.getDrawable(int) 获取它

      【讨论】:

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