How to integrate Firebase with Glide ('using' method) - java

I'm trying to use Firebase integration with Glide and for some reason,
Glide.using() cannot resolve this method.
I did add:
compile 'com.firebaseui:firebase-ui-storage:0.6.0'
Into build.gradle and also:
compile 'com.github.bumptech.glide:glide:4.0.0-RC1'
Here is the part which I'm trying to use Glide:
mStorageRef = FirebaseStorage.getInstance().getReference();
mStorageRef.child("images/Puffer-fish-are-pretty-damn-cute.jpg");
// Load the image using Glide
Glide.with(this)
.using(new FirebaseImageLoader()) // cannot resolve method using!
.load(mStorageRef)
.into(imageView);
I hope you can help me with that, didn't find any solutions online.

To solve this, please change this line:
compile 'com.github.bumptech.glide:glide:4.0.0-RC1'
with
compile 'com.github.bumptech.glide:glide:3.7.0'

Glide v4 is using module loaders with the annotation processor library.
Create AppGlideModule and then register FirebaseImageLoader. When loading images use StorageReference.
Here it is in details.
Add libraries in gradle
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
implementation 'com.firebaseui:firebase-ui-storage:4.1.0'
Extend the module and register
#GlideModule
public class MyAppGlideModule extends AppGlideModule {
#Override
public void registerComponents(#NonNull Context context, #NonNull Glide glide, #NonNull Registry registry) {
registry.append(StorageReference.class, InputStream.class, new FirebaseImageLoader.Factory());
}
}
Load images with ref
Uri uri = Uri.parse(photoUrl);
StorageReference ref = FirebaseStorage.getInstance().getReference().child(uri.getPath());
Glide.with(itemView.getContext())
.load(ref)
.into(thumb);

Related

Cannot resolve method of android.support.v4.app.FragmentActivity in ViewModelProviders

I am trying to implement architecture component in my application, but when I am to add lifecycleowner to my viewmodel inside my fragment with getActivity() its show message
Cannot resolve method of android.support.v4.app.FragmentActivity
This is my code:
viewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class);
viewModel.setToken(token);
viewModel.getRecent().observe(getActivity(), new Observer<List<Recent>>() {
#Override
public void onChanged(List<Recent> recent) {
adapter.setData(recent);
}
});
My fragment is derived from android.support.v4.app.Fragment
and MyActivity is extends AppCompatActivity
and this is my Gradle file:
def paging_version = "2.1.0"
def lifecycle_version = "2.0.0"
def room_version = "2.1.0-alpha04"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation "androidx.paging:paging-runtime:$paging_version"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
How do I solve this problem? thanks in advance.
Problem
In androidx.lifecycle.ViewModelProviders.of(FragmentActivity), they accept androidx.fragment.app.FragmentActivity, not android.support.v4.app.FragmentActivity.
Solution
Migrate entire project to AndroidX using Migrate to AndroidX which introduced from Android Studio 3.2. You can found information in this page
Change dependencies version to support library version. Dependencies version can be found this page
dependencies {
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version" // For Kotlin use viewmodel-ktx
// alternatively - just LiveData
implementation "android.arch.lifecycle:livedata:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData).
// Support library depends on this lightweight import
implementation "android.arch.lifecycle:runtime:$lifecycle_version"
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
// alternately - if using Java8, use the following instead of compiler
implementation "android.arch.lifecycle:common-java8:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "android.arch.lifecycle:reactivestreams:$lifecycle_version"
// optional - Test helpers for LiveData
testImplementation "android.arch.core:core-testing:$lifecycle_version"
}

Using Firebase Storage image with Glide

There are tons of duplicated answers I had tried almost all of them but I am still not able to use Firebase storage image with Glide.
First of all I am using docs
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference();
StorageReference pathReference = storageRef.child("sorular/1.jpg");
// ImageView in your Activity
ImageView imageView = rootView.findViewById(R.id.imageView);
// Load the image using Glide
Glide.with(this /* context */)
.using(new FirebaseImageLoader()) // Cannot resolve method 'using
.load(pathReference)
.into(imageView);
if I clean the .using part of Glide, logcat it gives this error:
E/GlideExecutor: Request threw uncaught throwable com.bumptech.glide.Registry$NoModelLoaderAvailableException: Failed to
find any ModelLoaders for model:
gs://123...appspot.com/sorular/1.jpg
at com.bumptech.glide.Registry.getModelLoaders(Registry.java:227)
at
com.bumptech.glide.load.engine.DecodeHelper.getLoadData(DecodeHelper.java:179)
at
com.bumptech.glide.load.engine.DecodeHelper.getCacheKeys(DecodeHelper.java:197)
at
com.bumptech.glide.load.engine.ResourceCacheGenerator.startNext(ResourceCacheGenerator.java:41)
at
com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:282)
at
com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:249)
at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:222)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
at
com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:347)
So how can use firebase storage images in my android app in a best way?
also this my build gradle dependencies:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:palette-v7:27.0.2'
implementation "com.android.support:cardview-v7:27.0.2"
implementation "com.android.support:recyclerview-v7:27.0.2"
implementation "com.android.support:support-v4:27.0.2"
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.github.florent37:materialviewpager:1.2.3'
implementation 'com.google.firebase:firebase-database:11.8.0'
implementation 'com.google.firebase:firebase-storage:11.8.0'
implementation 'com.firebaseui:firebase-ui-storage:2.0.1'
implementation 'com.google.firebase:firebase-auth:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Change this:
implementation 'com.firebaseui:firebase-ui-storage:2.0.1'
to this:
implementation 'com.firebaseui:firebase-ui-storage:3.2.1'
According to the Glide docs:
using()
The using() API was removed in Glide 4 to encourage users to register their components once with a AppGlideModule to avoid object re-use. Rather than creating a new ModelLoader each time you load an image, you register it once in an AppGlideModule and let Glide inspect your model (the object you pass to load()) to figure out when to use your registered ModelLoader.
To make sure you only use your ModelLoader for certain models, implement handles() as shown above to inspect each model and return true only if your ModelLoader should be used.
using() was removed from Glide 4.
To Solve this, you need to do this:
To load an image from a StorageReference, first register an AppGlideModule:
#GlideModule
public class MyAppGlideModule extends AppGlideModule {
#Override
public void registerComponents(Context context, Glide glide, Registry registry) {
// Register FirebaseImageLoader to handle StorageReference
registry.append(StorageReference.class, InputStream.class,
new FirebaseImageLoader.Factory());
}
}
Once you have created an AppGlideModule class and done a clean build, you can use GlideApp to load a StorageReference into an ImageView:
// Reference to an image file in Cloud Storage
StorageReference storageReference = ...;
// ImageView in your Activity
ImageView imageView = ...;
// Download directly from StorageReference using Glide
// (See MyAppGlideModule for Loader registration)
GlideApp.with(this /* context */)
.load(storageReference)
.into(imageView);
more info here: https://github.com/firebase/FirebaseUI-Android/tree/master/storage
I Know im bit late but it might help some of you.
Use both of these in app build.gradle.
implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0' //For Kotlin You should use kapt instead of annotationProcessor though.
Then add This Class:
#GlideModule
public class MyAppGlideModule extends AppGlideModule {
#Override
public void registerComponents(Context context, Glide glide, Registry registry) {
// Register FirebaseImageLoader to handle StorageReference
registry.append(StorageReference.class, InputStream.class,
new FirebaseImageLoader.Factory());
}
}
GlideApp.with(getActivity()).load(storageReference).into(profileImg);
At last you need to go to File-> Invalidate Cache and Restart
Done:)
If you've uploaded little images for icons on to your Firebase storage, get rid off glide and that "model". It makes a lot of changes on its git. So your code should look like:
StorageReference referenseLcl = FirebaseStorage.getInstance().getReference();
StorageReference islandRefLcl = referenseLcl.child(userLcl.getImageIconPath());
final long ONE_MEGABYTE = 1024 * 1024;
islandRefLcl.getBytes(ONE_MEGABYTE).addOnSuccessListener(bytesPrm -> {
Bitmap bmp = BitmapFactory.decodeByteArray(bytesPrm, 0, bytesPrm.length);
imageOfUser.setImageBitmap(bmp);
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
imageOfUser.setImageResource(R.mipmap.ic_launcher);
}
});
As for Glide 4.6.1 you can't use .using(new FirebaseImageLoader())
I am force to downgrade to
implementation 'com.github.bumptech.glide:glide:3.8.0'
and Firebase UI implementation'com.firebaseui:firebase-ui-storage:2.0.1'
The answers above didn't help me.
I was missing this in my gradle.
annotationProcessor 'com.github.bumptech.glide:compiler:4.x' //For Kotlin advice use kapt instead of annotationProcessor
The best docs I have found are here
I am using kotlin.
in my case, because I still use annotation processor like this
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
I should use:
kapt 'com.github.bumptech.glide:compiler:4.11.0'
Kotlin implementation of GlideModule :
#GlideModule
class GlideModule : AppGlideModule() {
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
registry.append(StorageReference::class.java, InputStream::class.java, FirebaseImageLoader.Factory())
}
}
Don't forget to import the correct dependencies :
dependencies {
def glide_version = "4.12.0"
implementation "com.github.bumptech.glide:glide:$glide_version"
kapt "com.github.bumptech.glide:compiler:$glide_version"
}

Annotation in Kotlin doesn't work

I'm trying to convert my Android app from Java to Kotlin.
For app shortcut I use shortbread library which is extremely easy to implement, but it doesn't work in Kotlin. Am I doing something wrong ?
Java:
#Shortcut(id = "Camera", icon = R.drawable.iconshortcut,longLabel = "Instant Scan", shortLabel = "Scan")
public class CameraActivity extends AppCompatActivity { ...
Kotlin :
#Shortcut(id = "Camera", icon = R.drawable.iconshortcut, longLabel = "Instant Scan", shortLabel = "Scan")
class CameraActivity : AppCompatActivity() { ...
Shortbread is working whenever the annotation is in Java, but not in Kotlin
Shortbread.create(this)
You'll need to use the kotlin-kapt plugin to be able to parse annotations in Kotlin files:
apply plugin: 'kotlin-kapt'
dependencies {
...
implementation 'com.github.matthiasrobbers:shortbread:1.0.2'
kapt 'com.github.matthiasrobbers:shortbread-compiler:1.0.2'
}
If that doesn't work it'd probably make sense to open an issue in the GitHub repo of the project.

Android room persistent: AppDatabase_Impl does not exist

My app database class
#Database(entities = {Detail.class}, version = Constant.DATABASE_VERSION)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract FavoritesDao favoritesDao();
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();
//Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
Gradle lib:
compile "android.arch.persistence.room:runtime:+"
annotationProcessor "android.arch.persistence.room:compiler:+"
And when i ask for instance it will give this error, AppDatabase_Impl does not exist
in my application class
public class APp extends Application {
private boolean appRunning = false;
#Override
public void onCreate() {
super.onCreate();
AppDatabase.getAppDatabase(this); //--AppDatabase_Impl does not exist
}
}
For those working with Kotlin, try changing annotationProcessor to kapt in the apps build.gradle
for example:
// Extensions = ViewModel + LiveData
implementation "android.arch.lifecycle:extensions:1.1.0"
kapt "android.arch.lifecycle:compiler:1.1.0"
// Room
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
also remember to add this plugin
apply plugin: 'kotlin-kapt'
to the top of the app level build.gradle file and do a clean and rebuild (according to https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#6)
In Android Studio, if you get errors when you paste code or during the build process, select Build >Clean Project. Then select Build > Rebuild Project, and then build again.
UPDATE
If you have migrated to androidx
def room_version = "2.3.0" // check latest version from docs
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
UPDATE 2 (since July 2021)
def room_version = "2.3.0" // check latest version from docs
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"
Just use
apply plugin: 'kotlin-kapt'
in app build.gradle
And keep both in dependencies
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion"
EDIT
In newer version don't need to add both dependencies at a time
Just use, hope it will work.
kapt 'android.arch.persistence.room:compiler:1.1.1'
I had this error when I missed
#Database(entity="{<model.class>})
Ensure that the entity model specified in the annotation above refers to the particular model class and also ensure that the necessary annotation:
#Entity(tableName = "<table_name>" ...)
is properly defined and you'd be good
if you are using kotlin classes to implement database then
use
apply plugin: 'kotlin-kapt'
and
kapt "android.arch.persistence.room:compiler:1.1.1"
in your gradle file, it will work.
For Kotlin Developers
Use this:
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
And add apply plugin: 'kotlin-kapt' to the top of the app level build.gradle.
For Java Developers
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
It is not just about updating your dependencies. Make sure all your Room dependencies have the same version.
implementation 'android.arch.persistence.room:rxjava2:1.1.0-alpha2'
implementation 'android.arch.persistence.room:runtime:1.1.0-alpha2'
annotationProcessor "android.arch.persistence.room:compiler:1.1.0-alpha2"
In the sample snippet above, all my Room dependencies have the same version 1.1.0-alpha2
Agreed with the above answers
The solution is as below. Change annotationProcessor to kapt as below
// annotationProcessor "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
I meet with the problem, because I forget #Dao annotation
#Dao
public interface SearchHistoryDao {
#Query("SELECT * FROM search_history")
List<SearchHistory> getAll();
#Insert
void insertAll(SearchHistory... histories);
#Delete()
void delete(SearchHistory history);
}
Room Official tutorial
make sure to add correct dependency for room in build.gradle
ext {
roomVersion = '2.1.0-alpha06'
}
// Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
And below line at the top-
apply plugin: 'kotlin-kapt'
I met this problem because I have forgotten the apt dependences
implementation "android.arch.lifecycle:extensions:$archLifecycleVersion"
implementation "android.arch.persistence.room:runtime:$archRoomVersion"
annotationProcessor "android.arch.lifecycle:compiler:$archLifecycleVersion"
annotationProcessor "android.arch.persistence.room:compiler:$archRoomVersion"
after added the annotationProcessor, and rebuild it, the problem solved.
If you are using kotlin, add kotlin annotation processor plugin to app level build.gradle
plugins {
id "org.jetbrains.kotlin.kapt"
}
Also remove annotationProcessor and replace it with kapt
Instead of
dependencies {
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
}
Use
dependencies {
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
}
The annotationProcessor only works in java environment. The kapt takes care of both java and kotlin. If something wrong with your implementation, those plugins will show them at the compile time.
Had the same problem. Implemented the few classes and interface as officially told in a new example project created by Android Studio:
https://developer.android.com/training/data-storage/room/
All mentioned solutions above did not help, the necessary _Impl files according to my database class were not generated by Room. Finally executing gradle clean build in terminal gave me the hint that lead to the solution:
"warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocation annotation processor argument OR set exportSchema to false."
I added the parameter exportSchema = false in the database class
#Database(entities = arrayOf(User::class), version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
And then it worked, found these two generated files in the app module under generatedJava:
AppDatabase_Impl
UserDao_Impl
I don't understand this behaviour as the parameter is said to be optional, see
https://stackoverflow.com/a/44645943/3258117
The question is pretty old, but I've stumbled on this today and none of the provided answers helped me. Finally I managed to resolve it by noticing that google documentation actually is still adopted to Java and not Kotlin by default, actually they have added a comment which I've ignored
For Kotlin use kapt instead of annotationProcessor
So, instead of
annotationProcessor "androidx.room:room-compiler:$room_version"
If you are developing with Kotlin, you should use:
kapt "androidx.room:room-compiler:$room_version"
In my kotlin app, I just added the following line at the top of my build.gradle file :
apply plugin: 'kotlin-kapt'
And the following line in the dependencies section:
kapt "androidx.room:room-compiler:2.2.5"
I hope it fixes your issue.
In my case, I was testing the connectivity for room database and I have put the testing class inside the directory which I have created inside the AndroidTest folder. I have moved it out of the custom directory, then it worked pretty well.
The same phenomenon occurred to me.
following
implementation "android.arch.persistence.room:runtime:1.1.1"
Adding causes another build error but tracks the cause from the log.
In my case, there was an error in the SQL implementation.
After fixing, the build was successful.
So you may want to check the implementation of the entire room library instead of looking at the crashed locals.
Use the following gradle link:
compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha9'
You need to create a different singleton class and get the AppDatabase from there like this:
RoomDB.java
public class RoomDB {
private static RoomDB INSTANCE;
public static AppDatabase getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();
//Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
App.java
public class App extends Application {
private boolean appRunning = false;
#Override
public void onCreate() {
super.onCreate();
RoomDB.getInstance(this); //This will provide AppDatabase Instance
}
The issue is more around the correct library that is not included in the gradle build. I had a similar issue and added the missing
testImplementation "android.arch.persistence.room:testing:$room_version
Changing the dependencies in my gradle file did'nt help me in fixing the error.I had missed this Database annotation in class where Room database was initialized which was causing this issue.
#Database(entities = [UserModel::class], version = 1)
Ensure that the entity model specified in the annotation above refers to the particular model class
For Kotlin Developers
if you checked Dao and Entity and also used Kapt and there is no problem, I guess there is a problem with your kotlin version if you are using kotlin 1.4 and above.
update Room to last version from this link.
2.3.0-alpha03 solved my problem.
For me, the Android Studio automatically updated dependencies as soon as you include any of the Room database related imports. But as per https://developer.android.com/jetpack/androidx/releases/room#declaring_dependencies you need to update few. Here is how my code-base looks like:
AppDatabase.kt
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
#Database(entities = arrayOf(MyEntity::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun myDAO(): MyDAO
companion object {
#Volatile private var instance: AppDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context)= instance ?: synchronized(LOCK){
instance ?: buildDatabase(context).also { instance = it}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(context,
AppDatabase::class.java, "db-name.db")
.build()
}
}
Update the build.gradle as specified in one of the answers:
apply plugin: 'kotlin-kapt' // this goes with other declared plugin at top
dependencies { // add/update the following in dependencies section
implementation 'androidx.room:room-runtime:2.2.3'
// annotationProcessor 'androidx.room:room-compiler:2.2.3' // remove this and use the following
kapt "androidx.room:room-compiler:2.2.3"
}
Sync the gradle and you should be good to go.
In addition to missing
annotationProcessor "android.arch.persistence.room:compiler:x.x.x"
I had also missed adding the below annotation in my class
#Entity(tableName = "mytablename")
Nothing works from above answers and I noticed that the issue persists for me when I'm using room version2.3.0 or 2.4.2. However, 2.5.0-alpha01 version works well when I applied it.
build.gradle:app
def roomVersion = '2.5.0-alpha01'
implementation "androidx.room:room-ktx:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
testImplementation "android.arch.persistence.room:testing:$roomVersion"
In my case just by changing annotationProcessor to kapt on my room-compiler dependency, did the work.
As of Jan 2023 - I faced a similar issue after refactoring my code to use ServiceLocator class.
I resolved it by going on a spree of changing room versions. It worked with 2.5.0-alpha02
version_room = "2.5.0-alpha02" <-- build.gradle (project)
//Room
implementation "androidx.room:room-runtime:$version_room"
kapt "androidx.room:room-compiler:$version_room"
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$version_room"
implementation("androidx.room:room-guava:$version_room")
I am running the following on Android Eel 2020.1.1:
version_kotlin = "1.7.21"
version_android_gradle_plugin = "4.0.1"
Reading the example here:
Room Example
I fixed this error just using the correct (I guess it is) annotationProcessorFile, as follows:
annotationProcessor "android.arch.persistence.room:compiler:<latest_version>"
Also, I upgraded to 2.2.0 either in Room Version as in Lifecycle version.
Once synchronized the graddle, I could start working with Room.
So, Good luck! And let the code be with you!
Not in the case of OP, but this also happens when you mistakenly use implementation instead of annotationProcessor like this:
implementation "android.arch.persistence.room:compiler:x.x.x"
Instead of this:
annotationProcessor "android.arch.persistence.room:compiler:x.x.x"
Just in case anyone out there should make the same mistake as I did, don't call your database class "Database" or you'll get the same error.
check this annotation above class
#Database(entities = arrayOf(Schedule::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
// ....
}

How to avoid boilerplate code when loading images with Picasso library

Let's say I have a layout with 4 ImageView's. Each ImageView must contain picture downloaded from remote url. Such task I can easily achieve with Picasso library like this:
Picasso.with(context)
.load(photo1Url)
.placeholder(R.drawable.image_view_placeholder)
.error(R.drawable.image_view_error_placeholder)
.centerCrop()
.tag(context)
.fit()
.into(feedListViewPhoto1);
Picasso.with(context)
.load(photo2Url)
.placeholder(R.drawable.image_view_placeholder)
.error(R.drawable.image_view_error_placeholder)
.centerCrop()
.tag(context)
.fit()
.into(feedListViewPhoto2);
Picasso.with(context)
.load(photo3Url)
.placeholder(R.drawable.image_view_placeholder)
.error(R.drawable.image_view_error_placeholder)
.fit()
.centerCrop()
.tag(context)
.fit()
.into(feedListViewPhoto3);
Picasso.with(context)
.load(photo4Url)
.placeholder(R.drawable.image_view_placeholder)
.error(R.drawable.image_view_error_placeholder)
.centerCrop()
.tag(context)
.fit()
.into(feedListViewPhoto4);
But maybe there is more compact solution to achieve such goal? eg.: to write only one time "Picasso with" and pass all required url's and ImageView object references? Maybe some kind of for cycle solution would help?
I strong recommend you to create a ImageLoader "service" inside your app.
First of all, create an Interface with the methods you need to download images and set into ImageView or to get bitmaps or features related:
public interface ImageService {
public void downloadAndSetImage(Context context, String url, ImageView image);
//Other important methods to you
}
With this, you can make implementatios with Picasso or other download systems. This is useful for example to change or test other systems without break your app.
After that create the implementation for the methods with all the boilerplate code.
public class PicassoImageImpl implements ImageService {
#Override
public void downloadAndSetImage(Context context, ImageDownloadInfo imageDownloadInfo) {
//All picasso code
}
}
Then in your application you only have to instanciate your ImageService (I strong recommend you to use DependencyInyection or at least a central Factory to get instances) and make simple calls to the method you need.

Categories