Loading an HTML file containing Images in a Textview - android

I am using textview to load the HTML text. The HTML text has images. I am using ImageGetter to load images. But the image is getting cropped in the right. Below is the code:
textView.setText(Html.fromHtml(htmlText, new ImageGetter() {
#Override
public Drawable getDrawable(String source) {
String path = "/sdcard/" + source;
Drawable bmp = Drawable.createFromPath(path);
bmp.setBounds(0, 0, bmp.getIntrinsicWidth(), bmp.getIntrinsicHeight());
return bmp; }}, null));

Related

Get SVG out of ImageView

I'm using AndroidSVG to dynamically place a signature rendered as an svg, for displaying to an ImageView.
Here is my code:
SVG svg = SVG.getFromString(singleEmployee.signature);
ImageView image_view = (ImageView) layout_signature.findViewById(R.id.toolboxtalk_signature_svg);
image_view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
image_view.setTag(String.format(Locale.US, "employee_signature%d", x));
Drawable drawable = new PictureDrawable(svg.renderToPicture());
image_view.setImageDrawable(drawable);
My question is, how do I get the SVG out of the ImageView later in my code execution.
Here is what I'm trying:
ViewGroup v1 = (ViewGroup) findViewById(R.id.job_ticket_form);
ImageView signatureImageView = (ImageView) v1.findViewWithTag("employee_signature1");
PictureDrawable pictureDrawable = (PictureDrawable) signatureImageView.getDrawable();
Picture picture = pictureDrawable.getPicture();
Context context = signatureImageView.getContext();
String employeeSigSVGString = null;
try {
SVG svg = SVG.getFromResource(signatureImageView.getContext(), R.id.toolboxtalk_signature_svg);
employeeSigSVGString = spinner_value + "|" + svg;
} catch (SVGParseException e) {
e.printStackTrace();
}
Any ideas how to get the SVG back out of the ImageView?

Set the text above the image in pdf cell using itext

I created the pdf using iText. In that, how to set the background image to the particular PdfCell and place the text in that same cell..
I set the background image to that cell by:
cell.addElement(image_green_left);
In case of both text and image, it show only image and text was hidden.
cell.addElement(image_green_left);
cell.addElement(new Paragraph("Test 3"));
table.addCell(cell);
How to show both text and image?
Create your own implementation of a PdfPCellEvent, for instance:
class ImageBackground implements PdfPCellEvent {
protected Image image;
public ImageBackground(Image image) {
this.image = image;
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
Image.scaleToFit(position.getWidth() , position.getHeight());
Image.setAbsolutePosition(position.getBottom(), position.getLeft());
cb.addImage(image);
}
}
Now when cell is the PdfPCell with the text and bgImage the path to your image, do this:
Image image = Image.getInstance(bgImage);
cell.setCellEvent(new ImageBackground(image));
Now, you'll have an image that acts as the background of the cell.

Html.fromHtml problem

I'm using Html.fromhtml to insert image in an editText ... but the instead of displaying the image, it displays [obj] " the text obj inside a square"
what could be the problem ??
This is the imageGetter
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = getResources().getDrawable(R.drawable.e11);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
This is the code line:
CharSequence cs =
Html.fromHtml("<img src='" +getResources().getDrawable(R.drawable.e11)+ "'/>"
,imageGetter, null);
A better way to do this would be to add a WebView.
Your imageGetter instance is not doing the correct thing. Where is it trying to retrieve the images from and what does it look like?

Android - How to show images from resources drawable?

I'm not able to show an image which is saved in res/drawable folder.
I use ImageGetter to do this. The code is below:
ImageGetter imageGetter3 = new ImageGetter() {
public Drawable getDrawable(String source) {
int id=0;
if (source.equals("smiley")) {
id = R.drawable.smiley;
}
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n"
+ "Sta met je rug voor de deur\n"
+ Html.fromHtml("<img src=\"smiley\">", imageGetter3, null) + " Draai naar links\n";
What I see on the screen when running is a little square with "obj" text on it.
So what is wrong? The image cannot be read or something else?
How to show images?
Honestly I have Googled a lot and tried other methods of ImageGetter as well, but none of them seems to work, I tried these too, they don't work:
ImageGetter imageGetter2 = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = null;
d = Drawable.createFromPath(source);
d.setBounds(0,0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawFromPath;
int path = Route.this.getResources().getIdentifier(source, "drawable","com.prj56.tracingapp");
drawFromPath = (Drawable) Route.this.getResources().getDrawable(path);
drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(), drawFromPath.getIntrinsicHeight());
return drawFromPath;
}
};
=========================================================
if (....) {
ImageView iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.smiley);
directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n"
+ "Sta met je rug voor de deur\n";
HERE COMES THE IMAGE! BUT HOW TO DO THIS? It's within directions textview...
directions += " Draai naar links\n";
}
Update 12 Dec 2016:
getResources().getDrawable() was deprecated in api 22 so you should now be using ContextCompat.getDrawable e.g.
Drawable d = ContextCompat.getDrawable(context, R.drawable.smiley);
In your activity you can call this to get your Drawable resource as a Drawable object
Drawable d = getResources().getDrawable(R.drawable.smiley);
If you want to show your drawable in an ImageView you can do this:
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.smiley);
or in your xml file
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/smiley"/>
You can get the image with the help of pImg.Based on the adapter position get the string that is the image name.The image should be present in the drawable folder.
ImageView prodImg=(ImageView)view.findViewById(R.id.img_proj_name);
String pImg=prod.get(position);
int resID = view.getResources().getIdentifier("#drawable/"+pImg , "drawable", parentView.getContext().getPackageName());
prodImg.setImageResource(resID);
A super easy way to show it is to write this in your ImageView widget in your xml:
android:background="#drawable/your_file_name"
... and make sure you have put that image file (whether png or jpg, etc.) into your drawable folder. When you write your_file_name, you only need the title, not the file extension.
I prefer this way over programmatically writing in your .java file (as shown with other answers above), because your xml will show you the image in preview, programmatically will not, it will just show the ImageView placeholder.
Easiest way - Can be consider the below code
We can take advantage of Imageview setImageResource , refer below code for the same.
put image in the drawable like the below order
image_1.png, image_2.png, etc.
int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", getPackageName());
imageview.setImageResource(resId);
Drawable drawable = getResources().getDrawable(R.mydrawableID);
Now you can use it as Image, Like:-
ImageView myImage = findViewById(R.id.yourImageView);
Now fire this -
myImage.setImageResource(drawable);
You should use ContextCompat for backward compatibility features on Android.
//Using ButterKnife
#BindView(R.id.DogImg)
ImageView myImage;
//Or
ImageView myImage = findViewById(R.id.MyImage);
...
Drawable MyImageDrw = ContextCompat.getDrawable(context,R.drawable.myImageOnResources);
myImage.setImageDrawable(MyImageDrw);

Android: showing default image in gallery's ImageView while actual image is downloaded

I believe this is pretty trivial but I can't get it to work.
I want to display a default image in gallery elements (ImageViews) while their actual image is being fetched from the net.
Right now, nothing is shown for an ImageView which its image has yet to arrive. Once it arrives it is immediately shown.
What I tried is right after the instantiation of the ImageView to call its setImageResource function like so:
final ImageView i = new ImageView(mContext);
i.setImageResource(R.drawable.loading);
But it doesn't seem to work. Below is the full getView() function.
Any help is appreciated.
Thanks.
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView i = new ImageView(mContext);
i.setImageResource(R.drawable.loading);
// if the drawbale is in the buffer - fetch it from there
Drawable bufferedImage = DataManager.getInstance().getImagesBuffer()[position];
if (bufferedImage != null){
i.setImageDrawable(bufferedImage);
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
}
// if drawable is not in buffer - fetch it from the net via AsyncImageLoader
else
{
String imageUrl = DataManager.getInstance().getImageBufferInstance().getImageUrl(position);
Drawable downloadedImage = AsyncImageLoader.getInstance().loadDrawable(imageUrl, new ImageCallback() {
public void imageLoaded(Drawable imageDrawable, String imageUrl) {
if (imageDrawable == null)
{
imageDrawable = getResources().getDrawable(R.drawable.icon);
}
i.setImageDrawable(imageDrawable);
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
}
});
i.setImageDrawable(downloadedImage);
}
i.setLayoutParams(new CoverFlow.LayoutParams(Utils.getInstance().getScreenWidth() / 2,
Utils.getInstance().getScreenHeight() / 2));
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return i;
}
Fix your indentation... If I'm reading that correctly, you're only setting the temporary drawable icon in the imageLoaded callback of your (not shown) AsyncImageLoader, which I assume means it's then only being set after the image downloads and is then immediately overwritten with the downloaded image. Try moving the placeholder-setting code into your else block outside the callback.

Categories

Resources