I'm trying to put the image from url and display it to textview. when i use case 2, the image display perfectly,
but in case 1 not. there is error "unknown protocol: data" and "W/AwContents: nativeOnDraw failed; clearing to background color"
//case 1
// String base_url = "<p>Image 1 : <img src=\"data:image/jpeg;base64,/9j/4AAQSk...
//case 2
String base_url = "<p>Image 1 : <img src=\"http://example.com/android/tryout/logo.png\"></img></p>";
Spanned span2 = Html.fromHtml(base_url,getImageHTML(),null);
TextView tv = (TextView)findViewById(R.id.target);
tv.setText(span2);
and this is my function
public Html.ImageGetter getImageHTML() {
Html.ImageGetter imageGetter = new Html.ImageGetter() {
public Drawable getDrawable(String source) {
try {
Drawable drawable = Drawable.createFromStream(new URL(source).openStream(), "src");
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
} catch(IOException exception) {
Log.v("IOException", exception.getMessage());
return null;
}
}
};
return imageGetter;
}
Thanks for the answer.
Related
if(myImageView.getDrawable() == null) {
return false;
}
else {
return true;
}
This tells if an image is attached with ImageView or not. I want to compare the Image which is residing in Drawable and Image which attached with ImageView.
private static final int WHITE_PAWN=R.drawable.wp,BLACK_PAWN=R.drawable.bp;
mSetOnClickListener(i,j,chess_array[i][j]);
protected void mSetOnClickListener(final int i,final int j,ImageView v){
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(isTroop(i,j)){
//for Events on ImageView
view.setBackgroundColor(getResources().getColor(R.color.select_troops_color));
}
}
});
}
protected boolean isTroop(final int i,final int j){
Drawable d=getBaseContext().getDrawable(BLACK_PAWN);
if(chess_array[i][j].getDrawable()==d){
return true;
}
else{
return false;
}
}
I have tried like this
Drawable d = getContext().getDrawable(R.drawable.your_drawable);
if(d == imageView.getDrawable()){
return true;
}
There are two ways you can do this. First way is you can assign your imageview in your XML with a TAG attribute and use the TAG attribute to determine which image it is. Of course you are not comparing the image here but you are comparing the tag.
XML
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="TEST"
android:src="#drawable/ic_launcher_background"/>
Java Class
imageView = findViewById(R.id.image);
if("TEST".equals(imageView.getTag())){
}
This will determine the image via the value of the TAG.
However, if you are determined to compare the image itself; then you'll need to convert the images into Bitmap and compare the bitmap against each other. Here's a example:-
imageView = findViewById(R.id.image);
final Bitmap myImage = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
final Bitmap comparingImg = ((BitmapDrawable) drawable).getBitmap();
if(myImage.sameAs(comparingImg)){
}
else{
}
Let me know if you need further clarification or if I am wrong.
I've been trying to add an image from drawable folder into a notification line. But the image doesn't appear and [OBJ] is shown instead.
I can add image in any other place on notifications. Also I can add emoji into a notification line like this issue Android notification - custom inboxstyle (add line ) .
But I can not add an image.
Is there any chance to overcome this issue?
public void someMethod() {
...
mInboxStyle.addLine(imageSpanned());
...
}
private Spanned imageSpanned () {
String imgString = "Something <img src=\"ic_arrow_forward_black_24dp\">";
return Html.fromHtml(imgString, mImageGetter, null);
}
Html.ImageGetter mImageGetter = new Html.ImageGetter() {
#Override
public Drawable getDrawable(String source) {
Drawable drawable;
Resources res = mContext.getResources();
int drawableId = res.getIdentifier(source, "drawable", mContext.getPackageName());
drawable = res.getDrawable(drawableId);
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, height);
return drawable;
}
};
You need to provide ic_arrow_forward_black_24dp icon resource in your drawables or mipmap.
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("Something").append(" ");
builder.setSpan(
new ImageSpan(
getActivity(),
R.drawable.ic_arrow_forward_black_24dp),
builder.length() - 1,
builder.length(),
0
));
textView.setText(builder);
I'm a beginner on Android Studio, and java, i want to create a random background of view images, i try to use this How To Display Random images on image view , but not working, this code doesn't generate any image
Please, can someone help me to resolve this code or to create another code to random background.
I use this code to generate images
final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rnd_images);
final ImageView img = (ImageView) findViewById(R.id.imgRandom);
// I have 3 images named img_0 to img_2, so...
final String str = "img_" + rnd.nextInt(2);
img.setImageDrawable
(
getResources().getDrawable(getResourceID(str, "drawable",
getApplicationContext()))
);
}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
}
is there anybody know how to convert a HTML page to an image in Android AsyncTask or background thread?
Environment: Android Studio 2.0 Beta 5, Android Tablets with API version 18, 19
I have tried WebView control with two solutions but all failed.
Solution 1: Javascript
It doesn't work, getDrawingCache(true) always returns null.
public void generateImage()
{
final WebView mWebView = new WebView(this);
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
mWebView.setInitialScale(150);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setDrawingCacheEnabled(true);
mWebView.addJavascriptInterface(new JavaScriptInterface(mWebView), "Android");
String html = "<html><body onload=\"Android.captureImage()\">hello world<p>bbb</p></body></html>";
mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
}
public class JavaScriptInterface
{
private WebView mWebView;
public JavaScriptInterface(WebView webView) {
mWebView = webView;
}
#JavascriptInterface
public void captureImage()
{
Bitmap b = mWebView.getDrawingCache(true);
if(b==null){
System.out.println("... b is null");
return;
}
System.out.println("... height "+b.getHeight()+" width "+b.getWidth());
}
}
Solution 2: WebViewClient + PictureListener
This one works on Android API 19. But on Android API 18, the callback "onNewPicture" is never get called.
public void generateImage()
{
final WebView mWebView = new WebView(this);
mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
mWebView.setInitialScale(150);
mWebView.getSettings().setLoadWithOverviewMode(true);
String html = "<html><body>hello world<p>bbb</p></body></html>";
mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
System.out.println("............ onPageFinished");
super.onPageFinished(view, url);
view.clearCache(true);
}
});
mWebView.setPictureListener(new WebView.PictureListener() {
#Override
public void onNewPicture(WebView view, Picture picture)
{
if (picture == null) {
return;
}
Picture pic = mWebView.capturePicture();
if(pic==null){
System.out.println("............ pic is null");
return;
}
System.out.println("onNewPicture............ width ["+pic.getWidth()+"] hieght ["+pic.getHeight()+"]");
final Bitmap bm = Bitmap.createBitmap(pic.getWidth(), pic.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
canvas.drawPicture(pic);
}
});
}
I can get image if I create WebView control in following way
final WebView mWebView = (WebView) findViewById(R.id.webview);
But I have to convert the html page string in an AsyncTask or a background thread. there is no UI at all. Is there anybody know how to implement it?
Any help is appreciated.
first set layout parameter for your WebView, second you should call Webview.measure and WebView.layout methods then draw cache on a bitmap:
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(final WebView view, String url) {
return false;
}
#Override
public void onPageFinished(final WebView view, String url) {
Log.i("HTML", "page finished loading " + url);
view.measure(View.MeasureSpec.makeMeasureSpec(512, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
final Bitmap bmp = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(view.getDrawingCache(), 0, 0, new Paint());
// bitmap is ready
}
);
IMPORTANT TIP: make sure your WebView height and width isn't zero.
One of my project activities including long text written in string.xml ,i add some images from resource within textview ,as after each phrase there is image then next the text phrase then image and so on ,
i getting that images from string using this code :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView htmlTextView = (TextView) findViewById(R.id.day_tv);
htmlTextView.setText(Html.fromHtml(getString(R.string.day), new ImageGetter(), null));
}
private class ImageGetter implements Html.ImageGetter {
public Drawable getDrawable(String source) {
int id = 0;
if (source.equals("image1.jpg")) {
id = R.drawable.a;
} else if (source.equals("image2.jpg")) {
id = R.drawable.b;
} else if (source.equals("image3.jpg")) {
id = R.drawable.c;
} else if (source.equals("image4.jpg")) {
id = R.drawable.d;
} else if (source.equals("image5.jpg")) {
id = R.drawable.e;
}
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
}
and writing html img tag in string.xml as:
<img src="image1.jpg">
i want to be able to customize each image by changing its width and height or refer it to drawable style to add border around images for example .
i tried using the bellow code foe changing width and height :
<img src="image1.jpg" width="100" height="150">
but it doesn't work .
any advice to achieve that will be appreciated , thanks .
I finally end up to this solution which allow to change width and height for each image but still one point not achieved which is referring image to drawable shape as background ,
still work on it :
private class ImageGetter implements Html.ImageGetter {
public Drawable getDrawable(String source) {
int id,id1,id2,id3,id4 = 0;
if (source.equals("image1.jpg")) {
id = R.drawable.a;
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, 80, 20);
return d;
}
else if (source.equals("image2.jpg")) {
id1 = R.drawable.b;
Drawable d1 = getResources().getDrawable(id1);
d1.setBounds(0, 0, 100, 40);
return d1;
}
else if (source.equals("image3.jpg")) {
id2 = R.drawable.c;
Drawable d2 = getResources().getDrawable(id2);
d2.setBounds(0, 0, 120, 60);
return d2;
}
else if (source.equals("image4.jpg")) {
id3 = R.drawable.d;
Drawable d3 = getResources().getDrawable(id3);
d3.setBounds(0, 0, 140, 80);
return d3;
}
else if (source.equals("image5.jpg")) {
id4 = R.drawable.e;
Drawable d4 = getResources().getDrawable(id4);
d4.setBounds(0, 0, 160, 100);
return d4;
}
return null;
};
}
}
The problem comes from Html.fromHtml(), which supports only a very limited set of tags and attributes. <img> is supported, but setting the width and height of the image is not. It's possible to achieve it with a TextView (by using SpannableString and ImageSpan), but it's more complicated and not very flexible.
Instead, you should use a WebView which handles HTML properly.
It's easy to load a string :
WebView webView = (WebView) findViewById(R.id.web_view);
webView.loadData(getResources(R.string.day), "text/html", null);
You can find more examples in the documentation.
Just for comparison, here is an example without using a WebView :
protected void onCreate(Bundle savedInstanceState) {
...
TextView textView = (TextView) findViewById(R.id.text_view);
SpannableString ss = new SpannableString("Hello :)");
// Draw the image with a size of 50x50
Drawable d = getResources().getDrawable(R.drawable.happy_face);
d.setBounds(0, 0, 50, 50);
// Replace the ":)" in the string by our drawable
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 6, 8, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(ss);
}
It will load much faster, but you can't use HTML.