Hi guys i have problem i need show image in EditText use : ImageGetter.
this work
String html = "<img src=\"ic_launcher\">";
CharSequence text = Html.fromHtml(html, new Html.ImageGetter(){
public Drawable getDrawable(String source){
int id = getResources().getIdentifier(source, "drawable", getPackageName());
Drawable d = getResources().getDrawable(id);
int w = d.getIntrinsicWidth();
int h = d.getIntrinsicHeight();
d.setBounds(0, 0, w, h);
return d;
}
}, null);
mContentEditText.setText(text);
but i need my image in SDcard ,not "R.drawable.IMAGE_NAME" ,Thanks
You have to apply the appropriate permissions in the manifest so that you can read from external storage. Then create a piece of code that will search your SD Card for the image you want.
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
Where imageFile is your ImageFile, for example: File imageFile = new File("/sdcard/gallery_photo_4.jpg");
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPath ="Your image Path here";
l1=(LinearLayout) findViewById(R.id.layout1);
l1.setBackgroundColor(Color.WHITE);
System.out.println(mPath);
Bitmap b=Bitmap.createScaledBitmap(BitmapFactory.decodeFile(mPath),l1.getWidth(),l1.getHeight(),false);
if(b!=null){
ImageView iv=new ImageView(this);
iv.setImageBitmap(b);
l2.addView(iv);
}
}
Here is the code which works for me.hope it will help you.
String html2 = "<img src=\"a.jpg\">";
CharSequence text2 = Html.fromHtml(html2, new Html.ImageGetter(){
public Drawable getDrawable(String source){
String path = "/sdcard/a/" + source;
File f = new File(path);
Drawable bmp = Drawable.createFromPath(f.getAbsolutePath());
bmp.setBounds(0, 0, bmp.getIntrinsicWidth(), bmp.getIntrinsicHeight());
return bmp;
}
}, null);
display.setText(text2);
is Work for me! 100% thx all :)
Related
I've got folder in assets called images1 with 114 images in it. I need to set them in listView with 2 textViews and 1 imageView. I haven't problems with textViews, but i don't know how to set images from assets to listView.
I tried:
int ids[] = new int[114];
for (int i = 0; i <ids.length; i++) {//<-------- taking ids of all pictures from images1 in assets-folder
try {
String[] images =getAssets().list("images1");
ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));
int imgId = getResourceId(this, listImages.get(i),"images1", getPackageName());
ids[i] = imgId;
}
catch(IOException ex) {}
}
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(
questionTexts.length);//<--------filling listView's textViews and imageView
Map<String, Object> m;
for (int i = 0; i < questionTexts.length; i++) {
m = new HashMap<String, Object>();
m.put(ATTRIBUTE_QUESTION_TEXT, questionTexts[i]);//<-------- textView
m.put(ATTRIBUTE_ANSWER_TEXT, answerTexts[i]);//<-------- textView
m.put(ATTRIBUTE_NAME_IMAGE, ids[i]);//<-------- imageView
data.add(m);
String[] from = { ATTRIBUTE_QUESTION_TEXT, ATTRIBUTE_ANSWER_TEXT,
ATTRIBUTE_NAME_IMAGE };
int[] to = { R.id.listView_item_title, R.id.listView_item_short_description, R.id.listView_image };
SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.item,
from, to);
lvSimple = (ListView) findViewById(R.id.lvSimple);
lvSimple.setAdapter(sAdapter);
}
public static int getResourceId(Context context,String variableName, String resourceName,
String packageName) throws RuntimeException{//<----- this method helps me to get IDs of images from assets/images1
try{
return context.getResources().getIdentifier(variableName,resourceName,packageName);
}catch (Exception e){
throw new RuntimeException("Error getting resource id");
}
}
But finally i've got white fields instead my pictures.
I know how to solve this problem when your pictures are in R.drawable, but how to do it when they are in assets subfolder?
You can use this.
try
{
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
ims .close();
}
catch(IOException ex)
{
return;
}
Are you sure that this images should be inside assets/ folder? Why not in res/drawables/?
Of course you can load it from assets ;)
To get the list of all files inside asset folder use below code:
list = getAssets().list("images1")
To load the image from asset you can use below code:
fun setImageFromAsset(ImageView view, String filename) {
try {
InputStream is = getAssets().open(filename);
Drawable drawable = Drawable.createFromStream(is, null);
view.setImageDrawable(drawable);
}
catch(IOException ex) {
return;
}
}
You can get a bitmap by using below code
private Bitmap getBitmapFromAssets(String fileName){
AssetManager am = getAssets();
InputStream is = null;
try{
is = am.open(fileName);
}catch(IOException e){
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
You can show bitmap in imageview by using "Glide" Here is sample code
Glide.with(context)
.load(Uri.parse("file:///android_asset/fileName"))
.into(imageView);
Simple Adapter can work only with IDs in drawable-folder or with Uri. But you can use ViewBinder to set image using setImageBitmap method.
I am creating the chat app , in which i am getting the EMOJI from the server (IMAGE URLS).
I am using this images(Emoji url) with text in my TextView by below lines of the code.
String stringWithHtml = "Sample string with an <img src=\"http://MY_SERVER.emoji.s3.amazonaws.com/cf68/5794d5f7895fa10a8f8e1350/imgList/5794d5f7895fa10a8f8e136a.png\"></img>" +
"<img src=\"http://MY_SERVER.emoji.s3.amazonaws.com/cf68/5794d5f7895fa10a8f8e1350/imgList/5794d5f7895fa10a8f8e135a.png\"></img>"+
"<img src=\"http://MY_SERVER.emoji.s3.amazonaws.com/cf68/5794d5f7895fa10a8f8e1350/imgList/5794d5f7895fa10a8f8e135b.png\"></img>";
Drawable drawable = Drawable.createFromStream(new URL(source).openStream(), "src name");
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
Spanned spannedValue = Html.fromHtml(stringWithHtml, drawable, null);
MY_TEXTVIEW.setText(spannedValue);
This all stuff , i am using in the AsynTask and getting the expected result like below:-
Now i am storing all the emojis(Images) on my device and want to use it with text in my TextView.
My question is that How can we use the device(Stored images) with text on my TextView ?
I have searched about it on SO but did not get the expected result.Please check below link which i have visited.
1. First Link
2. Second Link
3. Third Link
4. Forth Link
I have used the ImageSpanfor it but the other problem arises , which i have posted the question on SO Click here
Please help me to short out from this problem.Thanks 😊
OK. You even no need Uri. try this:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/car_icon.png";
String stringWithHtml = "Sample string with an <img src=" + path + "></img>"
+" <img src=" + path + "></img>"
+" <img src=" + path + "></img>";
Html.ImageGetter getter = new Html.ImageGetter() {
#Override
public Drawable getDrawable(String s) {
return BitmapDrawable.createFromPath(s);
}
};
Spanned spannedValue = Html.fromHtml(stringWithHtml, getter, null);
text.setText(spannedValue);'
You can implement a java program to get the count of the emoji's you receive.
And Dynamically create ImageView component in the UIView using the below program. Please use looping structures to repeat as much as you need.
ImageView iv = new ImageView();
RelativeLayout parentView = findViewById("R.id.parentViewId");
parentView.addView(iv, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Here is the code for loading images in textview from servers as well as from sdcard.. you can use sdcard related code :)
Spanned spanned = null;
String messageCustomized = "<img src ='"+ imageFileName +"'/>";
Spanned span = Html.fromHtml(messageCustomized, new
URLImageParser(sentMessagesViewHolder.tvMessage, context), null);
if (spanned!=null) {
spanned = (Spanned) TextUtils.concat(spanned, span);
}else spanned= span;
if (spanned!=null) {
txtView.setText(spanned);
}
ImageGetter
public class URLImageParser implements ImageGetter {
Context context;
View container;
private int imageSize = 20;
private int imageSizeDisplaySize = 20;
URLDrawable urlDrawable = null;
public URLImageParser(View container, Context context) {
this.context = context;
this.container = container;
imageSize = Utility.convertDpTopPixels(context, 20);
imageSizeDisplaySize = Utility.convertDpTopPixels(context, 35);
}
#Override
public Drawable getDrawable(final String fileName) {
urlDrawable = new URLDrawable();
Drawable drawable = null;
if (Build.VERSION.SDK_INT >= 21)
drawable = context.getDrawable(R.drawable.profile_main_placeholder);
else
drawable = context.getResources().getDrawable(R.drawable.profile_main_placeholder);
drawable.setBounds(0, 0, 0 + imageSize, 0 + imageSize);
urlDrawable.drawable = drawable;
Bitmap bitmap = null;
bitmap = ImageUtility.getImageFromSDCard(fileName);
if (bitmap != null) { // the bitmap is available
bitmap = RoundedImageView.getCroppedBitmap(bitmap, imageSize, imageSize, imageSize);
drawable = new BitmapDrawable(context.getResources(), bitmap);//ImageUtility.bitmapToDrawable(context,resource);
drawable.setBounds(0, 0, 0 + imageSize, 0 + imageSize); //set the correct bound according to the result from HTTP call
URLImageParser.this.urlDrawable.drawable = drawable;
}
return urlDrawable.drawable;
}
}
URLDrawable
public class URLDrawable extends BitmapDrawable {
protected Drawable drawable;
#Override
public void draw(Canvas canvas) {
// override the draw to facilitate refresh function later
if(drawable != null) {
drawable.draw(canvas);
}
}
}
You can use emojicon library , also refer here , sample source code included.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:emojicon="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<io.github.rockerhieu.emojicon.EmojiconTextView
android:id="#+id/txtEmojicon"
android:text="I \ue32d emojicon"
emojicon:emojiconAlignment="baseline"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<io.github.rockerhieu.emojicon.EmojiconEditText
android:id="#+id/editEmojicon"
android:text="I \ue32d emojicon"
emojicon:emojiconSize="28sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<fragment
android:id="#+id/emojicons"
android:layout_width="match_parent"
android:layout_height="220dp"
class="io.github.rockerhieu.emojicon.EmojiconsFragment"/>
</LinearLayout>
you can add custom emojis like below
EmojiconsView emojiconsView = (EmojiconsView) findViewById(R.id.emojicons_view);
emojiconsView.setPages(Arrays.asList(
new EmojiconPage(Emojicon.TYPE_PEOPLE, null, false, R.drawable.ic_emoji_people_light),
new EmojiconPage(Emojicon.TYPE_NATURE, null, false, R.drawable.ic_emoji_nature_light),
new EmojiconPage(Emojicon.TYPE_OBJECTS, null, false, R.drawable.ic_emoji_objects_light),
new EmojiconPage(Emojicon.TYPE_PLACES, null, false, R.drawable.ic_emoji_places_light),
new EmojiconPage(Emojicon.TYPE_SYMBOLS, null, false, R.drawable.ic_emoji_symbols_light)
));
}
I want to show an image, depending on the value of the int imgch
This is in my onCreate:
ImageView image = (ImageView)findViewById(R.id.gimage);
Resources res = getResources();
Drawable gameImage = res.getDrawable(images[imgch]);
And this in my XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gimage" />
How do I get it to show the correct image in the activity?
Yes, but with java-side code
ImageView img = (ImageView) findViewById(R.id.gimage);
img.setImageDrawable(getDrawable(R.drawable.my_image_name));
If you wanna load from an remote url you may be use this
https://github.com/nostra13/Android-Universal-Image-Loader
Check this ImageView#setImageDrawable. So you should have image.setImageDrawable(gameImage)
Create an Array
int[] images;
on
protected void onCreate(Bundle savedInstanceState) {
add this
images = new int[3];
images[0] = R.drawable.first_image;
images[1] = R.drawable.second_image;
images[2] = R.drawable.third_image;
and then your code (where imgch is an integer)
...
Drawable gameImage = res.getDrawable(images[imgch]);
...
use
Resources resources = getResources();
image.setImageDrawable(resources.getDrawable(R.drawable.myfirstimage));
I've been trying to make a small gallery and the bitmaps are always returning null. The code is like this:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
//Toast.makeText(getApplicationContext(),imgArray2.length+ " Image path from gallery : " + imgArray2[position], Toast.LENGTH_SHORT).show();
//Bitmap bitmap = BitmapFactory.decodeFile(imgArray2[position]);
//Uri uri = Uri.parse(imgArray2[position]);
//Bitmap bitmap = decodeFile(new File(uri.toString()).getAbsoluteFile());
//Bitmap bitmap = BitmapFactory.decodeFile(uri.toString());
//int imgID = getResources().getIdentifier(path, "drawable", "mypack.pack");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 15;
Bitmap bitmap = BitmapFactory.decodeFile(imgArray2[position], options);
//i.setImageResource(imgArray2[position]);
i.setImageBitmap(bitmap);
//Uri uri = Uri.parse(imgArray2[position]);
Toast.makeText(getApplicationContext(), "Image path from gallery : " + imgArray2[position], Toast.LENGTH_SHORT).show();
//i.setImageURI(Uri.parse(imgArray2[position]));
i.setLayoutParams(new Gallery.LayoutParams(170, 170));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
As you can see by the commented out code, I've been trying many option. setImageURI works but I need to scale down the image as I have many. The images are in the sd card. I checked the path of image and it is correct. What am doing wrong here?
Did you analyze the logs? Sometimes when images are too big to decode, the Dalvik VM will deny a memory request resulting in a null image being returned.
i need to show images from sqlite database into gridview or gallery view.
this is the code for displaying on a single view:
mMain = (ImageView) findViewById(R.id.ivMain);
byte[] blob = imgs.getBytes(); //there is a method that will return the bytes from the database
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
mMain.setImageBitmap(bitmap);
i have the android tutorial for grid view but it gets the image from file
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3
... }
is there a way to populate the gridview from the sqlite database?
UPDATE:
Im using the ImageAdapter provide in android tutorial:
http://developer.android.com/resources/tutorials/views/hello-gridview.html
my code becomes this:
ArrayList<byte[]> image_arr = new ArrayList<byte[]>();
//loop through all images on sqlite
for(int l = 0 ;l< db.getAllImages().size(); l++){
Image imgs = db.getAllImages().get(l);
byte[] blob = imgs.getBytes();
image_arr.add(blob);
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// mMain.setImageBitmap(bitmap);
}
//TODO; find way to make the bitmap get to the ImageView
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
This code is on main activity so i need to find way to pass the resources in ImageView which is in other file.
private void getDataAndPopulate() {
image = new ArrayList<byte[]>();
caption = new ArrayList<String>();
cursor=db.rawQuery("select * from NAME",null);
while (cursor.moveToNext()) {
byte[] temp_image = cursor.getBlob(2);
String temp_caption = cursor.getString(1);
String temp_id= cursor.getString(0);
image.add(temp_image);
caption.add(temp_caption);
id.add(temp_id);
}
String[] captionArray = (String[]) caption.toArray(
new String[caption.size()]);
ItemsAdapter itemsAdapter = new ItemsAdapter(Item_show_grid.this, R.layout.item_grid,captionArray);
gv.setAdapter(itemsAdapter);
}