Convert colorDrawable to bitmap - android

I am trying to save the profile picture from Google login, and am kind of a noob at this, so I am doing it in a rather round-about way. There is likely a better way to do this, and if you know it please let me know. But I think there is an unasked question in my round-about way. SO - here we go.
String profilePicUrl = googleSignInAccount.getPhotoUrl().toString();
ImageView intermediaryIV = (ImageView)
findViewById(R.id.intermediary);
Glide.with(this).load(profilePicUrl).into(intermediaryIV);
ColorDrawable drawable = (ColorDrawable) intermediaryIV.getDrawable();
Bitmap toSaveBitmap = drawable.get; //??
savePicToInternalStorage(toSaveBitmap)
The Glide api (https://github.com/bumptech/glide) loads the image from google into an imageView, and then I grab the drawable from the imageview. But in this case I don't get a normal bitmapDrawable I get a colorDrawable which does not contain a .toBitmap() function. Which causes me a problem because I need to save a bitmap.
So the question is can I convert a colorDrawable to a bitmap?
Also, if you have a better way to save an image from google, would you link me to that in the comments? We can leave the actually post answer to the main question.

You can store the bitmap from the image view drawing cache.
iv.buildDrawingCache();
Bitmap bitmap=iv.getDrawingCache();

Related

Apps keeps on crashing while adding 4th consecutive image when i use glide with Bitmap or Bitmap drawable

I was wondering about, how to implement Glide along with using Bitmap compression in Kotlin and thought that any of the code below would work. But unfortunately, the app closes as soon as I add an image into any of the imageView for the fourth time.
Here's the code which I tried to implement
val selectedImage = data?.data
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedImage)
//method 1
Glide.with(this).asBitmap().load(compressBitmap(bitmap,5)).into(imageView!!)
//method 2
var bitmapDrawable = BitmapDrawable( resources , compressBitmap(bitmap,5))
Glide.with(this).load(bitmapDrawable).into(imageView!!)
What would be the correct code, if i'm somewhere wrong here. Thankyou in advance
Try using Recycler View or another component to load the images in. They will handle the load.

Using frame by frame animation with Universal Image Loader

Is there any good way to use frame by frame animation AnimationDrawable using UIL library. AnimationDrawable accept only BitmpaDrawable . Is there any way to convert quickly to bitmaps my images or any maybe there is method like imageLoader.getBitmap, I haven't find anything like that.
Please help , I would be very grateful for any help .
Edit I have my images in assets folder. Maybe there any way to get bitmap from cache or something else . I need open new activity , maybe several times . I need to show animation from files , but if I use decode it takes a lot of time to decode them . Please suggest something
If your images are resources you can use this for obtain Bitmap:
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
else, if you want donwload the image you can use this:
String name = c.getString(url);
URL urlAux = new URL(name);
ImageView prueba = (ImageView)v.findViewById(R.id.iv_prueba);
if (prueba != null) {
Bitmap bitmap =
BitmapFactory.decodeStream(urlAux.openConnection().getInputStream());
prueba.setImageBitmap(bitmap);
}
Hope it helps you.

Pass imageView to next Activity- Android

I know this has been asked here, but my problem is a bit different. I read other answers related to this topic on SO but they did not solve my problem.
So in my app, I have 2 activities. In the first one, there are 20 imageviews (and I am planning to add more) and in the second activity, there is a custom view which is actually a zoom image class. Basically, when the user will click on any imageview in the first acivity he will be taken to the 2nd activity where he can zoom that particular image.
What I want to do in the coding part is that get the image that is displayed on the clicked imageview and pass it to the next activity. To achieve that, I have used the below given code but it's not working. What could be wrong?
Activity_one
Intent startzoomactivity = new Intent(Activity_one.this, Activity_two.class);
Bitmap img_res = img.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", img_res);
startzoomactivity.putExtras(extras);
startActivity(startzoomactivity);
Activity_two
Intent startzoomactivity = getIntent();
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
TouchImageView img = new TouchImageView(this); // TouchImageView is the class that enables zoom feature on the imageview.
img.setImageResource(i);
img.setMaxZoom(5f);
you cannot pass the ImageView to the other activity. Views are directly tied to the activity they belong and cannot be passed around.
What you're trying to achieve in the code is to get a drawing cache copy of the ImageView as a bitmap, which even after you make it work (it's missing the view.setDrawingCache), it will give you a very bad result, specially after zooming-in.
What you need to do is find the original image (if it came from the internet the URL, or the location in your disk cache, if it's from your resources folder, the int resId that draw that image, and pass this value (string or int) to the next activity. Then you let the next activity create its own ZoomableImageView and set the original resource (link, file, resource) to it.
I understand what you want to achieve but your way is totally wrong. Instead of passing ImageView Bitmap pass the ImageView URL or Path (and if drawable then ResId) where this image coming from and pass that URL or Path or ResId to the Next Activity and load this image as a Bitmap onto ZoomableImageView.
You can get the image from imageview as a bitmap, then compress it using http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream) api. then covert the outputstream object to byte array and since that is parcelable you can put this object in an intent and pass to your second activity

Save image to sdcard from imageview

I have listview having customized some textview and one imageview. When I long click on item I have store that item information to the database but the question is how to store image in sdcard and store the relevant path to the database. The image alread download as cache now I don't want to re-download that image.
Is there way to store that Image to the sdcard.
I am using this example to download the images for listview https://github.com/thest1/LazyList
Edit
I got solution
no need to extra process when using this example. Just store the web path of image into the database and pass that imageview into the ImageLoader object with path it'll use the cache images if the image was exist for same URL
No Need to extra process, Using this example it'll will care for future usage also for same URL of image. This will get from the cache directory if the image found that use that image otherwise download it and then use it.
If you use Prime the caching will be transparent, when you request the image again it will grab it from a memory cache if available or a disk cache automatically. It also is really easy to get images with.
You can save your bitmap which you get via Bitmap bitmap=memoryCache.get(url); using save-file-to-sd-card.
You can also get the bitmap from the ImageView(if you want) like:
//say your ImageView object is i;
i = (ImageView) findViewById(R.id.img);
Drawable d = i.getBackground();
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

Android Live Wallpaper guidance

When it comes to making Android live wallpaper, what are the things that are exactly needed. So far I could gist as WallpaperService.Engine, SurfaceView, some major changes in Android.manifest and xml/string.xml apart from this one png drawble (i think this may be optional if i use paint).
And what else do I need to develop whole different logic for the animation to happen? is it with mathematical calculations always? I am very keen about making different kind of live wallpaper. but i am not in the right track i think.
Please suggest me some way out to right direction summarizing me what all i need so that i can make any kind of live wallpaper.
Can somebody please summarize what all is needed for live wall paper.
Thanks in Advance.
Hi you can use this code if You have Image path.
is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(
bitmap, parent.getWidth(), parent.getHeight(), true);
bitmap.recycle();
if(imagePath!=null){
System.out.println("Hi I am try to open Bit map");
wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
wallpaperManager.setBitmap(useThisBitmap);
................................................. if you have image URI then use this
wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
mImageView.setImageURI(imagepath);
.............. Let me know if there is any issue .
Yes, you need to use a different approach for live wallpaper than "normal" animation in Android. The standard approach is to make a self-rescheduling runnable that draws to canvas.
In answer to one of your specific questions: no, you do not need to limit yourself to mathematical calculations; you can use bitmaps/sprites if you choose, but you will need to animate them yourself.
Your best place to start is the resources in the SDK:
http://developer.android.com/resources/articles/live-wallpapers.html
http://developer.android.com/resources/samples/CubeLiveWallpaper/index.html

Categories

Resources