Я пытаюсь добавить кнопку установки обоев в мое приложение, которая будет устанавливать обои, когда я получаю свое изображение из базы данных Firebase. Это то, что я до сих пор. Я установил кнопку вверх, чтобы она автоматически отображалась на каждом снимке обоев. Я установил обои в прошлом, когда картинка была сохранена на телефоне, но, похоже, не могу понять, как ее установить, когда вытащил изображение из базы.
public class WallpapersAdapter extends RecyclerView.Adapter<WallpapersAdapter.WallpaperViewHolder> {
private Context mCtx;
private List<Wallpaper> wallpaperList;
public WallpapersAdapter(Context mCtx, List<Wallpaper> wallpaperList) {
this.mCtx = mCtx;
this.wallpaperList = wallpaperList;
}
@NonNull
@Override
public WallpaperViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_wallpapers, parent, false);
return new WallpaperViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull WallpaperViewHolder holder, int position) {
Wallpaper w = wallpaperList.get(position);
holder.textView.setText(w.title);
Glide.with(mCtx)
.load(w.url)
.into(holder.imageView);
if(w.isFavorite){
holder.checkBoxFav.setChecked(true);
}
}
@Override
public int getItemCount() {
return wallpaperList.size();
}
class WallpaperViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, CompoundButton.OnCheckedChangeListener{
TextView textView;
ImageView imageView;
CheckBox checkBoxFav;
ImageButton buttonShare, buttonDownload;
Button setWallpaper;
public WallpaperViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text_view_title);
imageView = itemView.findViewById(R.id.image_view);
checkBoxFav = itemView.findViewById(R.id.checkbox_favorite);
buttonShare = itemView.findViewById(R.id.button_share);
buttonDownload = itemView.findViewById(R.id.button_download);
setWallpaper = itemView.findViewById(R.id.set_wallpaper);
setWallpaper.setOnClickListener(this);
checkBoxFav.setOnCheckedChangeListener(this);
/*buttonShare.setOnClickListener(this);*/
/*buttonDownload.setOnClickListener(this);*/
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_share:
shareWallpaper(wallpaperList.get(getAdapterPosition()));
break;
case R.id.button_download:
downloadWallpaper(wallpaperList.get(getAdapterPosition()));
break;
case R.id.set_wallpaper:
setWallpaper(wallpaperList.get(getAdapterPosition()));
break;
}
}
private void shareWallpaper(Wallpaper w){
((Activity) mCtx).findViewById(R.id.progressbar).setVisibility(View.VISIBLE);
Glide.with(mCtx)
.asBitmap()
.load(w.url)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
((Activity) mCtx).findViewById(R.id.progressbar).setVisibility(View.GONE);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(resource));
mCtx.startActivity(Intent.createChooser(intent, "The Wallpaper App"));
}
});
}
private Uri getLocalBitmapUri(Bitmap bmp){
Uri bmpUri = null;
try {
File file = new File(mCtx.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"the_wallpaper_app_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
private void downloadWallpaper(final Wallpaper wallpaper){
((Activity) mCtx).findViewById(R.id.progressbar).setVisibility(View.VISIBLE);
Glide.with(mCtx)
.asBitmap()
.load(wallpaper.url)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
((Activity) mCtx).findViewById(R.id.progressbar).setVisibility(View.GONE);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = saveWallpaperAndGetUri(resource, wallpaper.id);
if(uri != null){
intent.setDataAndType(uri, "image/*");
mCtx.startActivity(Intent.createChooser(intent, "The Wallpaper App"));
}
}
});
}
private Uri saveWallpaperAndGetUri(Bitmap bitmap, String id){
if(ContextCompat.checkSelfPermission(mCtx, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale((Activity) mCtx, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", mCtx.getPackageName(), null);
intent.setData(uri);
mCtx.startActivity(intent);
}else{
ActivityCompat.requestPermissions((Activity) mCtx, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100 );
}
return null;
}
File folder = new File(Environment.getExternalStorageDirectory().toString() + "/the_wallpaper_app" );
folder.mkdirs();
File file = new File(folder, id + ".jpg" );
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
return Uri.fromFile(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(FirebaseAuth.getInstance().getCurrentUser() == null){
Toast.makeText(mCtx, "Please login first", Toast.LENGTH_LONG).show();
compoundButton.setChecked(false);
return;
}
int position = getAdapterPosition();
Wallpaper w = wallpaperList.get(position);
DatabaseReference dbFavs = FirebaseDatabase.getInstance().getReference("users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child("favorites")
.child(w.category);
if(b){
dbFavs.child(w.id).setValue(w);
}else{
dbFavs.child(w.id).setValue(null);
}
}
}
private void setWallpaper(Wallpaper set) {
};
}
}