我想在我的Android应用中添加图片共享按钮,我正在从Firebase数据库获取图片

时间:2019-05-23 13:57:14

标签: android

我正在制作一个Android壁纸应用程序,在我的应用程序中,我从我的Firebase数据库中获取了一个壁纸图像,在我的应用程序中,我添加了两个按钮来保存和设置壁纸,现在我想在其中添加共享壁纸按钮我的应用程序,但我不知道如何添加直接从Firebase数据库共享图像的共享按钮。

公共类GamesSecond扩展了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() {

}

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

}

0 个答案:

没有答案