Android add shortcut icon from a url instead from drawable resources - android

I have an app where i am adding shortcut. I want to add shortcut icon image from URL path(images are stored on server and can be change) instead of using Drawable but i don't know how to do that. I tried to find a lot on this community. Any help?

If the term used "shortcut icon" is a view or its descendants such as ImageView, then you can set the image, your application receive from the server by doing this :
add dependency to your app.gradle file :
compile 'com.github.bumptech.glide:glide:3.7.0'
and in the activity you can use this method to set the image to the imageview your app receive from the server :
ImageView imageView = (ImageView) findViewById(R.id.image_view);
Glide.with(imageview.getContext()).load("https://url").error(R.drawable.not_found).centerCrop().into(imageview);

What you could do is first download that icon and store locally and then take that file path and use
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(this, UUID.randomUUID().toString())
.setIntent(intent) // !!! intent's action must be set on oreo
.setShortLabel(lable)
.setIcon(IconCompat.createWithAdaptiveBitmapContentUri(filePath)).build();
ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null);

add this in your build.gradle
compile 'com.squareup.picasso:picasso:2.5.2'
now you can load the image using following code
Picaso.with(this).load(urlHere).into(imageView);

Related

Is it possible to get icons from the server at run time?

I don't want to have some icons inside the application. Can I download icons from the server? And how can this be done? Maybe I need a cache for it.
Icons converted from svg to xml.
Yes,sure.. you can hit API and parse parameter named iconurl and put parameter in picasso.
Like this.
Picasso.with(context).load(your_iconurl).fit().into(holder.your_imageview);
Add below dependency in Gradle:
implementation 'com.squareup.picasso:picasso:2.71828'
Refer below sample code to load image into view:
String imageUri = "https://i.imgur.com/tGbaZCY.jpg";
ImageView ivBasicImage = (ImageView) findViewById(R.id.ivBasicImage);
Picasso.with(context).load(imageUri).into(ivBasicImage);
Reference:
http://square.github.io/picasso/
https://github.com/codepath/android_guides/wiki/Displaying-Images-with-the-Picasso-Library

Use drawable resources for Gboard sticker pack

Is it possible to use drawable resources bundled in your app instead of hosted images for adding stickers to Gboard?
Google provides the following code snippet here to show how to add a sticker to Gboard and it looks like the only way is to reference a hosted image:
new Indexable.Builder("Sticker")
.setName("Bye")
// add url for sticker asset
.setImage("http://www.snoopysticker.com?id=1234")
// see: Support links to your app content section
.setUrl("http://sticker/canonical/image/bye")
// Set the accessibility label for the sticker.
.setDescription("A sticker for Bye")
// Add search keywords.
.put("keywords", "bye", "snoopy", "see ya", "good bye")
.put("isPartOf",
new Indexable.Builder("StickerPack")
.setName("Snoopy Pack")
.build())
.build())};
All help is greatly appreciated!
String drawableResourceUri = Uri.parse("android.resource://your.package.name/drawable/yourfilenamewithoutextension").toString());
Add this string to your setUrl() method.
Google's AppIndexing sample project has an example of the files being generated at run time. This isn't exactly a Drawable resource but the same strategy can likely be used.

How to add Alphabetical profile pictures in android studio?

I want to add a default profile picture for my logged in user in my Android application. The way android marshmallow and above android version provides default alphabetical contact picture in contacts. There is a similar application "Micopi Pico". I want to use such functionality in my Android application to display the alphabetical profile picture as user's default profile picture. Is there any library or API or any method to do so? please help me.
There are many libraries to do that, here is an example, a library called Text Drawable (Click here to know more)
Import with Gradle:
repositories{
maven {
url 'http://dl.bintray.com/amulyakhare/maven'
}
}
dependencies {
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
}
Usage :
TextDrawable drawable = TextDrawable.builder()
.buildRect("A", Color.RED);
ImageView image = (ImageView) findViewById(R.id.image_view);
image.setImageDrawable(drawable);
You can go through the documentation to know more about the usage.
You should always try searching for a solution to your problem before asking a question in stack overflow.

Display Uri in ImageView Android

I have a Uri of captured image from camera as "file:///storage/emulated/0/Pictures/Colony_Quantifier/IMG_20160523_204353.jpg".
While I am trying to show that image in ImageView, I am not being able to do it no matter whichever options suggested in internet.
Uri uri = Uri.parse("file:///storage/emulated/0/Pictures/Colony_Quantifier/IMG_20160523_204353.jpg");
imgCaptured.setImageURI(uri);
Try with:
ImageView.setImageURI(Uri.fromFile(new File("/sdcard/cats.jpg")));
Or:
ImageView.setImageURI(Uri.parse(new File("/sdcard/cats.jpg").toString()));
You may use Picasso:
Include this line in your module level build.gradle dependencies:
compile 'com.squareup.picasso:picasso:2.5.2'
then use it as:
Picasso.with(yourContext).load(uri).into(yourImageView);
1- You create a Bitmap out of the URI
2- You create a new instance of BitmapDrawable passing in the Bitmap
3- You add the BitmapDrawable to a Window (setBackgroundDrawable), View (setBackgroundDrawable)] or ImageView (setImageDrawable)

show gif image in imageview Android

A very strange behaviour, while writing code for showing GIF image in image-view.
what i am doing is
InputStream is = this.getResources().openRawResource(R.drawable.logo);
logo is gif image that is in the drawable folder.
when I write that line of code in Eclipse , image loads successfully but when I import that code on Android Studio What I am seeing is an error on R.drawable.logo
and the error is
Expected Resource of type raw
supplying the wrong type of resource identifier. For Example when calling Resources.getString(int id), You should be passing R.string.something not R.drawable.something
one code runs on Eclipse not on Android Studio
I am sure that no problem with the imports.
After doing some R&D i have found a work-around for that
and that is instead of creating drawable folder and put that image into it
create assets folder in main/src/assests and paste that image and then open Input steam.
InputStream is = context.getAssets().open("logo.gif");
that worked for me but sill dont know the answer why
InputStream is = this.getResources().openRawResource(R.drawable.logo);
was not working.
you can try using ion https://github.com/koush/ion#load-an-image-into-an-imageview is a nice library for load images and support gif from local store , resorces or from Url.
Simple solution is using Glide library
compile 'com.github.bumptech.glide:glide:4.3.1'
And Load your ImageView
Glide.with(YourActivity.this).load(R.drawable.your_gif).into(yourImageView);

Categories

Resources