我想在我的应用程序中添加一个共享按钮,但我不知道如何实现它,我在我的应用程序中显示了Firebase存储中的图像

时间:2019-05-22 10:51:38

标签: android

我正在制作壁纸应用程序,在此应用程序中,我添加了三个按钮,例如“保存”,“共享”和“设置墙纸”。 “保存并设置墙纸”按钮可以正常工作,但问题出在“共享”按钮上,我想从该按钮实现共享按钮,用户可以在任何地方(如WhatsApp等)共享我的应用程序图像。

我把我的主要代码放在这个问题中

我的主要代码

public class GamesSecond extends AppCompatActivity {

    TextView mTitleTv;
    ImageView mImageIv;
    Button mSaveBtn, mWallBtn, mShareBtn;

    Bitmap bitmap;

    private static final int WRITE_EXTERNAL_STORAGE_CODE = 1;

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

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("ActionGames");
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);

        mTitleTv = findViewById(R.id.titleTv);
        mImageIv = findViewById(R.id.imageView);

        mSaveBtn = findViewById(R.id.saveBtn);
        mWallBtn = findViewById(R.id.wallBtn);
        mShareBtn = findViewById(R.id.shareBtn);

        String images = getIntent().getStringExtra("image");
        String title = getIntent().getStringExtra("title");

        mTitleTv.setText(title);
        Picasso.get().load(images).into(mImageIv);

        mSaveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                    if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
                            PackageManager.PERMISSION_DENIED){
                        String [] permission = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
                        requestPermissions(permission, WRITE_EXTERNAL_STORAGE_CODE);
                    }
                    else {
                        saveImage();
                    }
                }
                else {
                    saveImage();
                }
            }
        });

        mWallBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setWallpaper();
            }
        });

        mShareBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareWall();
            }
        });
    }

    private void shareWall() {

        <!-- HOW TO WRITE CODE HERE -->
    }

    private void setWallpaper() {

        bitmap = ((BitmapDrawable)mImageIv.getDrawable()).getBitmap();

        WallpaperManager MyWallManager = WallpaperManager.getInstance(getApplicationContext());
        try {
            MyWallManager.setBitmap(bitmap);
            Toast.makeText(this, "Wallpaper set...", Toast.LENGTH_SHORT).show();
        }
        catch (Exception e){
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    private void saveImage() {
        bitmap = ((BitmapDrawable)mImageIv.getDrawable()).getBitmap();

        String timeStamp = new SimpleDateFormat("yyMMdd_HHmmss",
                Locale.getDefault()).format(System.currentTimeMillis());
        File path = Environment.getExternalStorageDirectory();
        File dir = new File(path+"/Wallpaper Stack/");
        dir.mkdirs();

        String imageName = timeStamp + ".PNG";
        File file = new File(dir, imageName);
        OutputStream out;
        try{
            out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            Toast.makeText(this, imageName+" save to"+ dir, Toast.LENGTH_SHORT).show();
        }
        catch (Exception e){
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case WRITE_EXTERNAL_STORAGE_CODE:{
                if(grantResults.length > 0 && grantResults[0] ==
                        PackageManager.PERMISSION_GRANTED){
                    saveImage();
                }
                else {
                    Toast.makeText(this, "Enable permission to save image", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用类似这样的方式与Whatsapp共享图像:

 Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", imageFile));
    intent.setPackage("com.whatsapp");
    if (MyApplication.getAppContext().getPackageManager() != null && intent.resolveActivity(MyApplication.getAppContext().getPackageManager()) != null) {
        context.startActivity(intent);
    } else {
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.whatsapp")));
    }

这里的imageFile是存储在文件中的图像。

答案 1 :(得分:0)

On this page,您可以找到一个很好的例子。

// Share image
 private void shareImage(Uri imagePath) 
 {
   Intent sharingIntent = new Intent(Intent.ACTION_SEND);
   sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
   sharingIntent.setType("image/*");
   sharingIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
   startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));
 }