如何在我的应用程序中创建whatsapp共享选项

时间:2018-08-08 08:26:36

标签: android share-intent

我是Android开发的新手。我不知道Android Studio和Java。但是我正在Google,YouTube和StackOverflow的帮助下开发一个应用程序。现在,我想将Recyclerview图像中的共享选项(图像+文本)添加到Whatsapp。

使用Firebase lib从Picasso存储中加载图像。我不知道如何将Firebase ImageView转换为bitmap

我在每个项目下方添加了一个共享按钮。如何在此共享按钮中创建共享选项?

这是我的MainActivity:

public class ReviewMainActivity extends AppCompatActivity {

    private static final String APP_ID = "ca-app-pub-8867939169855032~3069406037";

    FirebaseDatabase database;
    DatabaseReference MCR;

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;

    FirebaseRecyclerAdapter<ReviewModel, ReviewViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review_main);

        Toolbar toolbar = findViewById(R.id.ReviewMain_Toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(null);
        toolbar.setLogo(R.mipmap.uploadpost);
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent settingsIntent = new Intent(ReviewMainActivity.this, ReviewPostUploadActivity.class);
                startActivity(settingsIntent);
            }
        });

        if (getSupportActionBar()!=null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        MobileAds.initialize(this,APP_ID);
        AdView adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
        database = FirebaseDatabase.getInstance();
        MCR = database.getReference("Review");

        recyclerView = (RecyclerView)findViewById(R.id.recycler_ReviewMain);
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager (layoutManager);

        loadMenu();
    }

    private void loadMenu() {
        adapter = new FirebaseRecyclerAdapter<ReviewModel, ReviewViewHolder>(ReviewModel.class,
                R.layout.review_main_items,
                ReviewViewHolder.class,MCR) {
            @Override
            protected void populateViewHolder(ReviewViewHolder viewHolder, ReviewModel model, final int position) {
                viewHolder.Review_Title.setText(model.getTitle());
                viewHolder.Profile_Name.setText(model.getProfileName());

                Picasso.with(getBaseContext()).load(model.getImage())
                        .into(viewHolder.Review_Image);

                Picasso.with(getBaseContext()).load(model.getProfileimage())
                        .into(viewHolder.Profile_Image);

                final ReviewModel local = model;

                viewHolder.Share_Icon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home)
            finish();
        return super.onOptionsItemSelected(item);
    }
}

这是我的意图部分

Intent shareIntent;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/Share.png";
OutputStream 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);
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent,"Share with"));

1 个答案:

答案 0 :(得分:0)

您可以尝试

Intent uriIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("whatsapp://send?text=" + "Text you want to share"));
startActivity(uriIntent);

这将打开whatsapp,并询问您要向其发送输入文本的人员。

要发送文件,请执行此操作

  File outputFile = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "example.pdf");
Uri uri = Uri.fromFile(outputFile);

Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.whatsapp");

activity.startActivity(share);      

以上代码将发送pdf文件。

要发送图像(一张图像),请将“ application / pdf”替换为“ image / *”

请记住:如果您尝试同时发送全部文本,pdf和图像,则应用程序将崩溃,因此一次只能发送一项。