I have made a custom keyboard that allows users to send GIFs/Images.
Currently, whenever the user taps on a ImageView, the following code runs. It checks whether the drawable inside the ImageView is animated (i.e - the image is a GifDrawable) and if not, it casts it to a BitmapDrawable. context.contentComitter.commit() is responsible for turning the casted drawables into files that can be read by other apps.
if(holder.imageView.drawable is GifDrawable) {
context.contentCommitter.commitGifDrawable(holder.imageView.drawable, fileName)
} else {
context.contentCommitter.commitBitmapDrawable(holder.imageView.drawable as BitmapDrawable, fileName)
}
The problem is - I'm getting the following crash for certain images on Samsung Galaxy S9s only:
Fatal Exception: java.lang.ClassCastException
android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
The solution seems to be simple - just perform another check for whether holder.imageView.drawable is a VectorDrawable, and if so, extract the Bitmap and write it to a file.
However, the problem is that the VectorDrawable source seems to be a GIF file, instead of a static image. This is confusing because the methods in the VectorDrawable documentation indicate that it is only used to store static images.
Is it possible for a VectorDrawable to contain animated images (i.e - GIFs), or do VectorDrawables only contain static images?
Here is the image database: https://github.com/vedantroy/Image-Database
Related
I am using Glide library on Android to load a JPG format image into an ImageView, first I convert it to a ByteArray and then I use the following code:
GlideApp.with(context)
.load(selectedImageByteArray)
.into(image_view)
However, when the selected image orientation EXIF data is equal to "Rotate 270 CW" the image is not rotated by Glide unless I use the following code:
GlideApp.with(context)
.load(selectedImagePath)
.into(image_view)
This way I pass the selected image Uri instead of a ByteArray, why does this happen?
I attach and example (even here in Stack is not rotated):
Because that interface takes a path and nothing else. Kind of annoying since depending on the api level it is not always available. In some of the newer versions of android it is not easy to get the actual path of the file.image
data for that
The short answer is that the data that interface uses is stored in the file, not in the image data itself. There are many stack overflow links about this:
SO
According to documentation here, A png resource should be converted to a BitmapDrawable. However I'm observing a strange behavior wherein a png file which has only black pixels in it is resulting in a crash because of ClassCastException (wrapped in InvocationTargetException) if I try to do the following in a Custom View's constructor :
...
tempDrawable = typedArr.getDrawable(R.styleable.CustomView_src); // Source points to a png file
Log.i("TestPNGToResource", "Canonical Class Name " + tempDrawable.getClass().getCanonicalName());
tempBitmap = ((BitmapDrawable) tempDrawable).getBitmap();
...
I see the following logged on android 2.2 and 2.3
09-24 13:21:37.575: I/TestPNGToResource(532): Canonical Class Name android.graphics.drawable.ColorDrawable
Why is the resource not being converted to a BitmapDrawable?
This is an optimization performed by aapt. It is able to recognize images made of a single color and turn them into, well, a single color instead of a bitmap. This is more space-efficient, improves loading times, etc.
i assume this won't occur for 9-patch images, right? also, when using the image file, will extracting the apk file still show the image file inside it?
anyway, i'm not sure if that helps, but have you tried to check this value:
TypedValue value = a.peekValue(R.styleable.CustomView_src);
then , if it's any of the color types, you can create your own drawable with this color. problem is how to get the size if that's what you get.
however, if in the special case you've shown, you get a reference type, you can probably force it to be BitmapDrawable by just creating the bitmap using BitmapFactory.decodeResource().
I'm currently trying to make an android dicom app
Following code opens pictures drom res/drawable in "ussual" image formats, but it doesn't work with .dcm
public class BitmapView extends View
{
public BitmapView(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.test);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, 10, 10, null);
}
}
in the main activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new BitmapView(this));
}
thanks in advance!
Dicom is a kind of generic container. Inside a Dicom file you can find a huge variety of image formats. From grayscale ones to RGB, from single frame to multiframe ones, with pixel value ranges not in the ordinary 8 bit (24/32 in RGB/RGBA) but also in 12 or 16 bit grayscales.
Dicom files include many elements (fields) indicating the type of the contents and even how such contents should be presented. It is not as simple as converting the Dicom image to BMP.
If you are retrieving Dicom images from a PACS, I would suggest using WADO service. This way, you can obtain Jpeg images (the results of having applied a presentation state to the contents of the Dicom file).
The other option is to use some utility to convert the Dicom file to a more conventional image format. There are some excellent open source tools, such as dcmj2pnm, from the DCMTK toolkit.
Dicom images are not recognized by Android and therefore you cannot open them using a BitmapFactory object.
Recently Imebra has been extended with Java wrappers that call the Imebra native code and comes with a pre-built JAR so you don't have to deal with JNI compilation.
In addition to the 2 options mentioned by jap1968 and Paolo, which are both good but require using some method for converting the image on a different platform then viewing it on the Android device and an internet connection, you can actually view the Dicom file itself on the Android device directly if you use a library that supports DCM and its different sub-types. One such toolkit has a free demo application on Google Play here:
https://play.google.com/store/apps/details?id=leadtools.datasetdemo
I'm noticing a crash (in an external native library that does some image processing) when I pass it the pixel data returned from bitmap.getPixels().
If I package the image in the app, in the drawables folder and load the Bitmap with
BitmapFactory.decodeResource()
then grab the pixel data with
bitmap.getPixels()
there's no crash, and everything works as expected. However, if I load the same image from the file system with
BitmapFactory.decodeFile()
then grab the pixels with
bitmap.getPixels()
and hand that off, the native lib crashes.
Is there a difference between the way these two calls process the image into a Bitmap?
Reading the Android sources There is one interesting diffrence: The decodeFile method may call a different native bitmap decoder if the passed file is an asset, while the decodeResource will never do this.
if (is instanceof AssetManager.AssetInputStream) {
bm = nativeDecodeAsset(((AssetManager.AssetInputStream) is).getAssetInt(),
outPadding, opts);
However, the crash is most likely a bug in your native code. Messing up the stackframe with bad pointers and/or buffer overruns typically results in weird crashes like this. Try to check all your native code that runs before the crash and see if you can spot any memory issues like that.
I am facing problem while setting the backGround Image of LinearLayout from asset folder.
String filePath="file:///android_asset/images/test.png";
Drawable d = Drawable.createFromPath(filePath);
frontTextView.setBackgroundDrawable(d);
Can someone help me out.
First you create a Drawable object from the asset file:
Drawable d = Drawable.createFromStream(getAssets().open(path_in_assets), null);
and then set it to some View that only supports Drawables as a background.
As far as I'm aware, you cannot access assets directly like you are trying to. You'll need to use the AssetManager class to get at your data if you want to store it as an asset. Here's a pretty decent blog post explaining a bit about resources and assets, though the official documentation is also a good resource, of course.
I'll also add, though, that things like background images are typically best stored in res/drawable and accessed using the R.drawable.* style (the blog post linked above also discusses this) whenever possible. It's not really clear why you need to do it this way from your provided code sample, though, so I suppose that's ultimately your call.
EDIT: added create image from InputStream...
I had the similar problem using ImageButton. I figured it out by loading bitmap from assets and using it as image for ImageButton. Probably not a good approach, but is working and solved my problem - unability to have subfolders in drawable dir and not allowed characters in file names.
(Yes, I can use prefix instead of subdir, and rename files to match the pattern (lowercase only and numbers) and I probably will do it later.)
InputStream is = null;
try {
is = this.getResources().getAssets().open("Images/Fruits/Apple.png");
} catch (IOException e) {
Log.w("EL", e);
}
Bitmap image = BitmapFactory.decodeStream(is);
ImageButton ib2 = (ImageButton) findViewById( R.id.imageButton2);
ib2.setImageBitmap( image);