how to set up a wallpaper app using firebase recyclerview? - java

I created a wallpaper app ,I'm using Firebase to upload images to a database and shown it to RecyclerView.
I can see the images that I uploaded in Firebase through the RecyclerView and I can also pass that image to a another activity.
but my problem is, I can't set the image as wallpaper, when I set the button the image is disappearing.
my code to set wallpaper:
img = (ImageView) findViewById(R.id.images);
Intent intent = getIntent();
String webUrl = intent.getStringExtra("URL");
Picasso.get().load(webUrl).into(img);
fab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WallpaperManager wallpaper = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaper.setResource(+ R.drawable.pug); //by using this code i can set a image in directory a wallpaper
//wallpaper.setResource(+ R.id.images); //i tried this one it doesn't work it just crashes the app
}catch (IOException e){
e.printStackTrace();
}
}
});
what I want is to set any image shown in the ImageView
is there any way

I'm assuming you already have the permission in place for this:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
I'm not sure what you are trying to achieve, but I am assuming you want the user to choose an image from the list and set it as wallpaper.
You can only pass a Bitmap or Resource ID to WallpaperManager. In this case, fetch the image as a Bitmap first, and then load into the ImageView.
Since you are already using Picasso, you can do this:
Picasso.with(this).load(webUrl).centerCrop().into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
wallpaper.setBitmap(bitmap);
}
}

Related

ImageButton call setImageDrawable() but get wrong size

I was writing an android application which contains a RecyclerView inside my ActivityMain. Each rows has an imageButton and a textLabel. In onBindViewHolder() method, I download an image from an URL and after using setImageDrawable method, I change the image with the one that I just downloaded.
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
holder.textAuthor.setText(posts.get(position).getAuthorName());
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
// Get URL Image from my ArrayList Posts(which contains imageButton and textLabel)
URL url = new URL(posts.get(position).getImageView());
// Get inputStream from URL
InputStream inputStream = (InputStream) url.getContent();
// Create a drawable which contains my downloaded image
Drawable drawable = Drawable.createFromStream(inputStream, null);
// Change the image with the new one
holder.imageButton.setImageDrawable(drawable);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
All my ImageButton change image correctly downloading from their URL, but the problem is that they have wrong size until I scroll down and after a while I scroll up inside of my RecyclerView using my finger. It sounds like if I have to refresh my View but I don't know how to do this.
Note: all images have different size
I let you some images to explain better my problem:
When I open my application and it starts to download image from URL
When the download is completed the images have wrong size
If I scroll down and after I scroll up I get the images with the correct size
The rest of my codes is here: MainActivty MyAdapter PostClass
XML File which contains ImageButton: my row of RecyclerView
Hope you can help me as always, thank you a lot!
EDIT: I have fixed downloading image using Glide!
Try the following code it worked for me
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="25dp"
android:layout_height="25dp"
app:srcCompat="#drawable/about" />
give your image button fixed height and width then it will work

How can receive icons from URL by Picasso and show it in the first visit not in rerun the app?

By using information in stackoverflow I finally could write a code that receive an image from URL and set it as hamburger menu icon in navigation drawer by using Picasso . It works very well . But still there is a problem : ( when installing the app for the first time and run it ) OR ( when turning off the mobile and then rerun the app ) , the icon is not shown . When rerun the app ( before turning off the mobile ) , the app run correctly and icon is shown .
How I can solve this problem?
{
final Target mTarget = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap,Picasso.LoadedFrom loadedFrom) {
Log.d("DEBUG", "onBitmapLoaded");
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
mBitmapDrawable = new BitmapDrawable(getResources(), scaledBitmap);
getSupportActionBar().setHomeAsUpIndicator(mBitmapDrawable);
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable drawable) {
Log.d("DEBUG", "onPrepareLoad");
}
};
Picasso.get().load("http://192.168.1.53:8080/Farid/1.jpg").into(mTarget);
}
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
You may create a local cache for the icon, after downloaded the icon you might convert it to float and store it in sharePreference, when the icon cannot load, you might convert the cached icon from float to bitmap from sharePreference. This thread might help you Store bitmap to SharePreference

How to check image resource of an imageview programmatically?

I want to check background of my imageview and do sth if it equals to some drawable in my drawable folder. How can I do that? I have try this code but no answer:
layoutRoot.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (view.getBackground() == R.drawable.grid_single_bg_selected) {
//Do sth
}
}
});
When you set the background drawable also do set a tag to the imageview
imageView.setTag(R.drawable.demo);
And then compare
if (((Integer) imageView.getTag()) == R.drawable.demo){
// Do stg
}
If you are setting the imageView programmatically, while setting, also set a tag to this imageView. Now, when you want to find out the drawable, you can do it by retrieving the tag of this imageView.

Android: Picasso enlarging Image on Click?

I was using Picasso for a while now and I'm very satisfied with it. However I'm in need of enlarging an imageview's image which was loaded by Picasso. As in, I simply want to create a zoomImageFromThumb of the image. However the traditional zoomImageFromThumb requires a resource to be present in the project. In my case, Picasso loads it up.. so how do I enlarge an image laoded by Picasso on click of the image view?
My code so far:
ImageBanner= (ImageView) findViewById(R.id.lb_single_image);
Picasso.with(ctx)
.load("www.flickr.com/randomimage.jpg")
.placeholder(R.drawable.loading_placeholder)
.error(R.drawable.error_placeholder)
.into(ImageBanner);
ImageBanner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//how do I enlarge the picasso image?
}
});
Instead of setOnClickListener() you can use PhotoViewAttacher:
ImageBanner= (ImageView) findViewById(R.id.lb_single_image);
Picasso.with(ctx)
.load("www.flickr.com/randomimage.jpg")
.placeholder(R.drawable.loading_placeholder)
.error(R.drawable.error_placeholder)
.into(ImageBanner);
PhotoViewAttacher mAttacher = new PhotoViewAttacher(ImageBanner);
Only one line, and it will works.

Launch crop image activity before setting wallpaper

In my application users can click a button to set an image from imageview as wallpaper.
Here is the code:
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(
((BitmapDrawable) fullSizeImage.getDrawable()).getBitmap());
//setResource(fullSizeImage.getDrawable());
Toast.makeText(
FullSizeImageDisplay.this,
"Wallpaper set",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
It is setting the image as wallpaper, but not working properly on all devices, where only a part of the picture is set as wallpaper.
I want user to get an option to crop the image before setting it as wallpaper, like Android shows before setting an image as wallpaper from Gallery.
Can that activity from Gallery be launched from my app to crop image or is there another alternative?
You could start an intent in onActivityResult after loading Image. There You could start the cropping with this intent, for example:
Intent cropYourPicIntent = new Intent(
"com.android.camera.action.CROP");
But this is only to get the idea of it what I mean, there is a lot of more code to do.

Categories