简体   繁体   中英

How to transform uri to integer for drawable?

I am working on widgets and i need to use this views.setImageViewResource method in AppWidgetProvider but this method wants integer but i have uri how can i solve this problem?

class AppWidgetBroadcast : AppWidgetProvider() {
private lateinit var database: DatabaseReference
private lateinit var mydatabase: FirebaseDatabase

override fun onUpdate(
    context: Context,
    appWidgetManager: AppWidgetManager,
    appWidgetIds: IntArray
) {
    // Perform this loop procedure for each widget that belongs to this
    // provider.
    appWidgetIds.forEach { appWidgetId ->
        // Create an Intent to launch ExampleActivity.
        val pendingIntent: PendingIntent = PendingIntent.getActivity(
            /* context = */ context,
            /* requestCode = */  0,
            /* intent = */ Intent(context, WidgetDetailsActivity::class.java),
            /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        // Get the layout for the widget and attach an on-click listener
        // to the button.
        val string="sa"
        val uri:String="myUri"
        var imageuri: Uri = Uri.parse(uri)
        val views: RemoteViews = RemoteViews(context.packageName,R.layout.activity_widget_details).apply {
            setOnClickPendingIntent(R.id.fullImageWidget, pendingIntent)


        }



        // Tell the AppWidgetManager to perform an update on the current
        // widget.
        //views.setImageViewUri(R.id.fullImageWidget,null)

        views.setImageViewUri(R.id.fullImageWidget,imageuri)
        appWidgetManager.updateAppWidget(appWidgetId, views)
    }
}

}

If your Uri is of this type: android.resource://your.package.name/drawable/drawable_name

So you use this code in your project. It will help you.

public Drawable getDrawableFromUri(Activity activity, Uri uri) {
        try {
            InputStream inputStream = activity.getContentResolver().openInputStream(uri);
            return Drawable.createFromStream(inputStream, uri.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

OR

If your Uri is of this type: https://url.image.jpg

So you use Glide library in your project for load Image.

  • build.gradle
repositories {
  google()
  mavenCentral()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.14.2'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.14.2'
}
  • activity.java
 String URL = "https://media.istockphoto.com/id/1162019476/tr/fotoğraf/oyuncak-yarış-arabasında-boy-ve-köpek.jpg?s=1024x1024&w=is&k=20&c=TxRP45SO7Eja8IKQ0KFqHZAbzXuvWWVD3sLsQUpSc-A="

 Glide
    .with(YOUR_CONTEXT)
    .load(URL)
    .into(YOUR_IMAGE_VIEW);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM