I want to get Bitmap from ImageView. I have used following code, but getDrawable() returns null. How to get whole Bitmap from ImageView.
Bitmap bitmap;
if (mImageViewer.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageViewer.getDrawable()).getBitmap();
} else {
Drawable d = mImageViewer.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
storeImage(bitmap,"final.jpeg");
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)
Could be that the emulator or device your using has a different density and is trying to pull images from another folder.
If you are using an extension in your image other than .png, .jpg, or .gif, It might not recognize other extension types. http://developer.android.com/guide/topics/resources/drawable-resource.html
According to this answer, just do it like this:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
For Kotlin:
simply write this code to get the bitmap from an ImageView
imageview.invalidate()
val drawable = imageview.drawable
val bitmap = drawable.toBitmap()
If you are trying to get bitmap from Glide loaded image then this will help you
Drawable dr = ((ImageView) imView).getDrawable();
Bitmap bmp = ((GlideBitmapDrawable)dr.getCurrent()).getBitmap();
Take a picture of the ImagView and convert it to a string to send to the server
ImageView ivImage1 = (ImageView ) findViewById(R.id.img_add1_send );
getStringImage( ( ( BitmapDrawable ) ivImage1.getDrawable( ) ).getBitmap( ) ),
public String getStringImage(Bitmap bm){
ByteArrayOutputStream ba=new ByteArrayOutputStream( );
bm.compress( Bitmap.CompressFormat.PNG,90,ba );
byte[] by=ba.toByteArray();
String encod= Base64.encodeToString( by,Base64.DEFAULT );
return encod;
}
Related
I have some problem with my code..
I want to set value luckywheelitem library from https://github.com/thanhniencung/LuckyWheel ,
I have image file from gallery then convert to bitmap
String root = Environment.getExternalStorageDirectory().toString() + "/Luckywheel";
Bitmap bitmap = BitmapFactory.decodeFile(root+ "/sarada.png" );
Then i want set value luckyItem1.icon = ; but just receive integer value
how to convert from image bitmap to int (drawable)
Thank you for attention
You should pass to luckyItem1.icon only a resource file. You should add drawable to your project and then use it.
Example:
luckyItem1.icon = R.drawable.luckyItemIcon
we need to merge two images with background large image and in front of that a small bitmap and I am using this code then the result is like this what is wrong and how to solve this problem. here the image of the small image is not visible but in small image place a large image is placed
**
Java code
**
Bitmap backgroundimage = BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.rec_);
BitmapDrawable mBitmapDrawable= new BitmapDrawable( overlay(bitmap,backgroundimage));
mBitmapDrawable.setGravity(CENTER);
toolbar_layout.setBackground(mBitmapDrawable);
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}
I am trying to convert an OpenCV mat to android Bitmap but that gives me images with a bluish tint(yellow colour turns to blue)! Even though I am not doing any processing on the image! I have no clue why this is happening. Below is some relevant code:
File file = new File(imgDecodableString);
image = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_COLOR);
resultBitmap = Bitmap.createBitmap(image.cols(), image.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(image, resultBitmap);
Bitmap mResult = resultBitmap;
ImageView imgView = (ImageView) findViewById(R.id.imgView);
imgView.setImageBitmap(mResult);
//imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
I am new to android app development so I might be missing something simple. Thanks for help!
EDIT:
I am uploading images for reference.
Based on the given suggestions, I made a function which converts Mat to Bitmap. This function works perfectly.!
private static Bitmap convertMatToBitMap(Mat input){
Bitmap bmp = null;
Mat rgb = new Mat();
Imgproc.cvtColor(input, rgb, Imgproc.COLOR_BGR2RGB);
try {
bmp = Bitmap.createBitmap(rgb.cols(), rgb.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgb, bmp);
}
catch (CvException e){
Log.d("Exception",e.getMessage());
}
return bmp;
}
As suspected, the issue is with the RGB color convention, Android follows RGB color convention, but OpenCV follows BGR color convention, You can rectify it using Imgproc.cvtColor(mat, Imgproc.COLOR_BGR2RGBA), before displaying it in the ImageView.
I need to convert BitmapDescriptor to Bitmap.
I can convert a bitmap to BitmapDescriptor with this code:
BitmapDescriptor bd = BitmapDescriptorFactory.fromBitmap(bitmap);
but how can i do the inverse way ?
I need to get BitmapDescriptorFactory.defaultMarker() in bitmap format.
I m not sure but I don't think so it is possible
In my application i have requirement to change default market color ...i got the default marker as bitmap using below code hope it helps u
Drawable d = DrawableUtils.resizeImageToDrawable(
MapViewFragment.mapViewActivity,
Configuration.Display.getDrawableFix(i),
Configuration.MapView.getWaypointIconWidth(),
Configuration.MapView.getWaypointIconHeight());
d.setColorFilter(color, Mode.MULTIPLY);
Bitmap b = ((BitmapDrawable) d).getBitmap();
Canvas myCanvas = new Canvas(b);
int myColor = b.getPixel(0,0);
ColorFilter filter = new LightingColorFilter(myColor, color);
If any questions u can ask
I am using Universal-Image-Loader and there is this functionality that access the file cache of the image from sd card. But I don't know how to convert the returned file cache into bitmap. Basically I just wanted to assign the bitmap to an ImageView.
File mSaveBit = imageLoader.getDiscCache().get(easyPuzzle);
Log.d("#ImageValue: ", ""+mSaveBit.toString());
mImageView.setImageBitmap(mSaveBit);
Error: "The method setImageBitmap(Bitmap) in the type ImageView is not applicable for the arguments (File)"
You should be able to use BitmapFactory:
File mSaveBit; // Your image file
String filePath = mSaveBit.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
Define File
String fileName = "/myImage.jpg";
File file = new File(fileName);
get Bitmap of Image
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
Set Bitmap to ImageView
myImageView.setImageBitmap(bitmap);
You can use this function to get Bitmap from file path
fun getBitmap(filePath:String):Bitmap?{
var bitmap:Bitmap?=null
try{
var f:File = File(path)
var options = BitmapFactory.Options()
options.inPreferredConfig = Bitmap.Config.ARGB_8888
bitmap = BitmapFactory.decodeStream(FileInputStream(f),null,options)
}catch (e:Exception){
}
return bitmap
}
Here is a simple code to create a scaled image for ImageView in this case
- Width:400
- Height:400
final File file = new File(Environment.getExternalStorageDirectory(),"b.jpg");
ImageView img = (ImageView) findViewById(R.id.imageview);
img.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()),400,400,false));
Kotlin Version
if (requestCode==PICK_IMAGE_REQUEST){
if (data!=null){
selectedfileUri=data.data
if (selectedfileUri!=null && !selectedfileUri!!.path.isEmpty()){
val file = FileUtils.getFile(context,selectedfileUri)
val bitmap = BitmapFactory.decodeFile(file.path)
uimg!!.setImageBitmap(bitmap)
}
}
}
This is not the right question, but if you use flag .cacheInMemory() in ImageLoader setup you can retrive the bitmap without need of recreate at any time using BitmapFactory to safe memory usage .
Just use:
Bitmap bitmap = ImageLoader.getInstance().getMemoryCache()·get("url as key");