PainterResource throws IllegalArgumentException: Only VectorDrawables and rasterized asset types are supported - android

I am working on a Jetpack Project which downloads an image from API using Coil Library.
I have confirmed that valid image URLs and related data are being returned from the API call successfully. I am able to view the image on my browser using the returned URL.
This is my Image Composable:
#Composable
fun AstroPhoto(picture: AstroPicture) {
val imgUrl = picture.url.toUri()
.buildUpon()
.scheme("https")
.build()
AsyncImage(model = ImageRequest.Builder(LocalContext.current)
.data(imgUrl)
.size(Size.ORIGINAL)
.crossfade(true).build(),
placeholder = painterResource(R.drawable.loading_animation),
contentDescription = picture.title,
contentScale = ContentScale.Crop,
)
}
I call the above image composable in a Lazy Column:
....
Box( ... ) {
LazyColumn(content = {
items(state.astroPictures) {
AstroPhoto(picture = it)
} })
}
This is the exception at I am getting:
FATAL EXCEPTION: main Process: com.uxstate, PID: 31790
java.lang.IllegalArgumentException: Only VectorDrawables and
rasterized asset types are supported ex. PNG, JPG
at androidx.compose.ui.res.PainterResources_androidKt.loadVectorResource(PainterResources.android.kt:93)
I am on compose_version = '1.1.1' working with kotlin_version = '1.6.10'. My coil version is "io.coil-kt:coil-compose:2.1.0" and compileSdk 32 and AS Chipmunk. Source code can be found here.
I have checked the docs and searched online but cannot resolve the error. Please help me to go about the error, Thanks.

if you are using png and jpg with painterResource but still face this issue. Change your image, It happened when there is something wrong with the image.
painterResource converts png, and jpg in the bitmap. Its throws that error if it fails to convert any Resource

My project worked correctly before, but after I changed some code (just add a fucntion, it is not about png or jpg or other things about image), my app crash when render a png. But after i reinstall the project in the emulator, it is fixed.
It seems like a bug in Compose? I dont know, may be a bug in gradle?
Just try to reinstall your app, i think it will be fixed.

in my previous projects, there is no problem passing drawable resources to painterResource function. But when I create a new jetpack-compose project, the problem you describe occurs.
In my new project, this problem only happens in preview phase. When I firstly build and run my app in emulator, the problem suddenly disappears.
so this is my solution: if you haven't run you app in emulator, build and run it in emulator.

Just encountered this error and see if anyone had the same error as me.
I just re-opened the Android Studio, and after that, I use Clean Project and Rebuild Project. Now the project and the compose preview are working fine.

Tried to use shape drawable as a placeholder and it caused this error.
If I set the same placeholder to ImageRequest.Builder instead, the code works fine.
AsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(imageUrl)
.placeholder(R.drawable.placeholder_rect)
.error(R.drawable.placeholder_rect)
.build(),
contentDescription = "",
)

Related

Why does Jetpack Compose preview show the wrong string resources?

Sometimes when I use a string resource in my composable, the preview will show the wrong string. It always works fine for literal strings, only string resources are wrong. The bug isn't consistent.
For example if I have this strings.xml:
<resources>
<string name="app_name">Violit</string>
<string name="load_topic_failure_message">Something went wrong loading the topic</string>
</resources>
And I have this composable:
#Composable
fun TopicFailureContent() {
Text(stringResource(R.string.load_topic_failure_message))
}
#Preview(showBackground = true)
#Composable
fun TopicFailureContentPreview() {
TopicFailureContent()
}
It might render something like "Partially checked" or "Navigation menu" instead of "Something went wrong loading the topic".
If I change the composable to this:
#Composable
fun TopicFailureContent() {
Text("Something went wrong loading the topic")
}
it renders the preview correctly.
It looks like the preview might be rendering nearby strings instead of the one I want. String resources work fine in tests and running the app. It's just preview that is not always working.
I'm using Android Studio Electric Eel 2022.1.1 but I was having the same problem on the previous version as well. This fails on both Compose UI version 1.2.1 and 1.3.3.
Any idea why string resources don't always work in preview and how to fix it?

Adding a style to custom text view in compose

I have a CustomTextView, I used to add style to it using xml style attribute, but with compose I need to understand how to do it.
When using this :
AndroidView( factory = { context->
RegularAmountTextView(ContextThemeWrapper(context,R.style.TextAppearance_MyTheme_Heading_Small ),null ).apply {
text ="1234"
}
}, modifier = Modifier.fillMaxWidth().wrapContentHeight() )
I get error: Infinite cycle trying to resolve '?attr/textColorHighlight': Render may not be accurate. in preview panel.
while on running on device I get:
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}
at android.content.res.TypedArray.getColor(TypedArray.java:529)
at android.widget.TextView.readTextAppearance(TextView.java:4345)
at android.widget.TextView.<init>(TextView.java:1340)
at android.widget.TextView.<init>(TextView.java:1258)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:108)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:103)
Similar error only. If I remove the style then it works. Has Android team provided any solution for this.
Another question: On facing these errors I was thinking is using compose on production a wise decision, or we should wait till android team provides proper inter-portability.

Finding import and dependency with Android Studio

I am trying to use imageResource in an Android Jetpack Compose app but I cannot find the depndency to add to my grade file.
imageResource is in the docs here:
androidx.compose.ui.res
Image(
bitmap = imageResource(id = R.drawable.background),
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop
)
When I add androidx.compose.ui.res to my grade file it fails to resolve.
How do I find the dependency to add to my grade file for immageresouzrce?
No dependency, the link you posted clearly states the method is accessible though ImageBitmap.Companion.imageResource(...), so that is what it is.
Type the exact phrase and you'll be good.
Explicitly using the Companion property is not necessary, you could directly use ImageBitmap.imageResource(...) here as well.

AndroidX ExifInterface can read camera make/model but not lens make/model

I'm building an app which reads EXIF data from images and overlays that data on the image so you can share your camera settings with a nice graphic rather than manually typing them out (EG: "F/1.4 at 1/200 ISO400")
I'm using AndroidX ExifInterface 1.1.0-beta01 and the blow code works to get every piece of data except the LensMake and LensModel are always null.
I've tried reverting to ExifInterface 1.0.0 and that made no difference, it still behaves identically.
I note that the documentation for ExifInterface refers to LensMake and LensModel as returning an "ASCII String" which Camera Make and Camera Model just return a "String" so i've tried different variations of getAttribute without success.
These exact files work fine on the iOS version of the app I've previously built and i've tried files from multiple different cameras (Fuji X-T3, Canon 5D III)
var stream: InputStream? = null
try {
stream = contentResolver.openInputStream(uri)
val exifInterface = ExifInterface(stream!!)
FS = exifInterface.getAttribute(ExifInterface.TAG_F_NUMBER)!!
SS = exifInterface.getAttribute(ExifInterface.TAG_EXPOSURE_TIME)!!
ISO = exifInterface.getAttribute(ExifInterface.TAG_PHOTOGRAPHIC_SENSITIVITY)!!
val LensMake = exifInterface.getAttribute(ExifInterface.TAG_LENS_MAKE) //THIS APPEARS TO BE ALWAYS NULL :(
val LensModel = exifInterface.getAttribute(ExifInterface.TAG_LENS_MODEL) //THIS APPEARS TO BE ALWAYS NULL :(
val CameraMake = exifInterface.getAttribute(ExifInterface.TAG_MAKE)
val CameraModel = exifInterface.getAttribute(ExifInterface.TAG_MODEL)
}
I'd like to be able to read the lens information, I know it's in the file but this library doesn't seem to want to expose it.
There is an open bug filed on the issue tracker, which states, that:
Although the constants are available for LensMake and LensModel, the getter does not return the actual values from the file. It seems like proper support is missing. I think the reason is that ExifTag[] IFD_EXIF_TAGS does not contain an array item for lens make and model. Adding the following lines at the right place of the aforementioned array, seems to fix things:
new ExifTag(TAG_LENS_MAKE, 42035, IFD_FORMAT_STRING),
new ExifTag(TAG_LENS_MODEL, 42036, IFD_FORMAT_STRING),
Not sure how reliable this is, but it is at least a solution approach.

Iconize Xamarin with Fontawesome

I'm new to Xamarin and I'm trying to use the Iconize NuGet package for Xamarin, but I'm not having much luck. At the moment I'm working with a simple Android app. I've installed Iconize per the instructions and, save for an error on the line:
FormsPlugin.Iconize.Droid.IconControls.Init(Resource.Id.toolbar, Resource.Id.tabs);
(the compiler didn't like the Resource.Id.toolbar or Resource.Id.tabs so I removed it) everything compiles and runs. However, when I try to add an IconButton with a Fontawesome image, I get "System.NullReferenceException: Object reference not set to an instance of an object." error that points back to
the line LoadApplication( new App() ); in the MainActivity.cs.
I'm trying to add the IconButton to a grid in code (not XAML) using
grid.Children.Add( new IconButton
{
Image = "fa-info-circle"
}, 3, 2 );
Any ideas on how to make this work? The examples on the Iconize page haven't been very useful and I haven't found any examples on Google.
Okay I finally found something that was useful. I found a clue on this page and some other information on the project issues page on Github.
Ultimately, to get the icon to display I used
grid.Children.Add( new IconButton
{
Text = "fa-info-circle",
}, 3, 2 );
It was the Text property and not the Image property that I should have used.
Hope this helpful to someone else.

Categories

Resources