Getting images to dislay using fromHtml in Android Studio - java

I am currently using fromHtml in Android Studio to display a webpage's text and images in a TextView. While I am able to load the text just fine, displaying the images doesn't seem to work as expected, leaving small cyan squares in place of where images should be when running the application. After doing some research, I came up with a way I thought would work to display these images as seen below.
content.setText(Html.fromHtml(newContent.content, new Html.ImageGetter() {
#Override public Drawable getDrawable(String source) {
System.out.println(source);
Drawable drawFromPath;
Picasso.get().load(source).into(loadedImage);
drawFromPath = loadedImage.getDrawable();
return drawFromPath;
}
}, null));
Picasso should load the image into a temporary drawable of sorts and return it each time an image is encountered. I know it is finding the images as printing out source prints out the proper image url. However, I can't seem to get it to connect to my app and fill in these cyan squares. Any ideas?

Related

View to Bitmap to PDF rendering only on second attempt & massive file- How to resize with my configuration?

I want my PDF to render properly and instantly as they normally render, on first try & with reasonable file size and resolution.
Actual results:
Clicking "print button" the first time produces a blank page in print adapter. This also takes around 5 seconds to render. Clicking "print button" a second time takes 5 seconds or more to finally load the PDF. Additionally, the PDF file is a staggering 2.16MB for a 1 page output.
I believe this must be a memory problem, understandably.
What I'm doing:
I have a relatively large view fully rendered, its invisible to user and in a print friendly color sceme to be printed. It measures 900dp X 300dp. This is a sort of tableview/scrollview dependency, pretty much a super simple spreadsheet with some numbers. I load this view into a View to Bitmap capturing dependency. I then load this into the android provided print helper for printing a Bitmap.
Dependency:
https://github.com/HelloHuDi/ViewCapture
My button click code:
printableVersion.setOnClickListener {
printChartCreate(unitDimensionImperialEven.text.toString().toInt()) // Create chart
val bitmap: Bitmap? = ViewCapture.with(printableTableMetricCoursing).bitmap // I guess this initializes bitmap from chart
bitmap.set
this.also { context ->
PrintHelper(context).apply {
scaleMode = PrintHelper.SCALE_MODE_FILL
}.also { printHelper ->
printHelper.printBitmap("droids.jpg - test print", bitmap!!)
}
}
}
So far I've tried implementing bitmap.createBitmap() method so to recreate the bitmap with a lower resolution but get errors and crash with everything i've tried. The current resolution/inch on inspection is 520dpi. How can I reduce the resolution with this configuration above to 300 or less? I believe this smaller file will solve all the above problems.
EDIT: I've also tried 3 different bitmap capturing dependencies and this is the same output or crashes.

A button which allows an image to be shown in Android Studio

I am building my application using Android Studio, this app can upload an image from raspberry to my emulator. It works fine. What I want to do now is uploading this image and showing it directly to the user without searching it in the gallery. I thought about creating another class and setting this image as a background image in my xml file, but this is too much like I have to create another class every time I want to upload an image from my raspberry.
Can someone help me please. Thank you
If I'm understanding your question correctly, you'd like to load an image from the Android filesystem into your app and display it to the user.
Drawable, Android's generalized image class, allows you to load from file via Drawable#createFromPath.
This SO question suggests Drawable#createFromPath doesn't work on paths beginning with file://, so depending on your use case you may want to precede that with Uri#parse/Uri#getPath.
Once you have a Drawable, you can display it in one of two ways: put an ImageView in your app and call its setImageDrawable method, or set the Drawable as your background image via View#setBackground (note that setBackground was only added in API 16 - in prior versions, you should call View#setBackgroundDrawable).
Putting all of this together, we end up with the following (untested):
private void loadImage(String imagePath) {
Uri imageUri;
String fullImagePath;
Drawable image;
ImageView imageDisplay;
imageUri = Uri.parse(imagePath);
fullImagePath = imageUri.getPath();
image = Drawable.createFromPath(fullImagePath);
imageDisplay = (ImageView) findViewById(R.id.imageDisplay);
/*if image is null after Drawable.createFromPath, this will simply
clear the ImageView's background */
imageDisplay.setImageDrawable(image);
/*if you want the image in the background instead of the foreground,
comment the line above and uncomment this bit instead */
//imageDisplay.setBackground(image);
}
You should be able to modify this to work with any View just by replacing imageDisplay's declared type with the appropriate View type and changing the cast on findViewById. Just make sure you're calling setBackground, not setImageDrawable, for a non-ImageView View.

Most efficient way to load a lot of images from URL Android

I'm making this netflix style app in which images are loaded into different categories. Let's say Dog videos (has 15 images), Cat videos (15 images), etc... All the images are loaded from a URL, it kind of takes a while for all to load. I was wondering if there was anything I could do to speed up the process? Or maybe show an empty container then fill it as the images load (that would be cool).
This is what I have done:
I have multiple async calls in one Activity, (1 async call per category)
JSONTask1 dogTask = new JSONTask1();
JSONTask2 catTask = new JSONTask2();
JSONTask3 pigTask = new JSONTask3();
JSONTask4 horseTask = new JSONTask4();
dogTask.execute("");
catTask.execute("");
pigTask.execute("");
horseTask.execute("");
I have all of those in a row in my actual code. Thanks.
I would use the "proxy pattern". Basically, you need to create a class that contains the minimal informations required for the display. In which, you have a preview image.
When ever you load everything you start by showing the preview content, ie : a loading gif for everypicture with the title of the movie or whatever. and basically the proxy would have a "loadImage" method that would make an ajax call or async call and the photos would load one by one. Plus, to make the loading easier, make sure the photos are not oversized.
You can see Picasso answers , in picasso i suggest you this way :
Picasso.with(getApplicationContext()).load("your url").placeholder(R.drawable.your_place_holder).error(R.drawable.showing_when_error_occured)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
}
});
Also another suggestion from me : convert your thumb images to base64 format in backend, then firstly retrieve your thumbs and show them. Then start an async task and change images when successfull.
Like whatsapp. In whatsapp you have thumb images they have so low resolution and super fast. When you click image if you have internet connection they load actual thumb images, and click again they load larger image.
Picasso website :http://square.github.io/picasso/
Load them asynchronously with Picasso, you can even show a placeholder image until the real one is loaded

is it right to use batcher.drawSprite to draw all images in game?

I'm developing a game and use batcher.drawSprite method to draw all images in the game (background and all characters)
in assets.java :
charAtlas = new Texture(game, "charAtlas.png");
charEnemy = new TextureRegion(charAtlas, 0,0,250,300);
in worldGame.java :
batcher.beginBatch(Assets.charAtlas); // set atlas
batcher.drawSprite(130, 628, 120,140, Assets.charEnemy);
//assets.charEnemy
is it right to use this method in all condition ?
I have 3 atlas in game , i even use 2048x2048 atlas size so i can include all my images in there..
However, the image looks blurry in game (Tested in galaxy note, tab, and galaxy young). looks at the code above, i even have the enemy char take size in my atlas as much as 250x300 , it's not make sense that it'll look blurry as i only draw it in 120x140.
note : i use no layout (i mean no layout file in res folder) .. i use drawsprite to draw all image (Character,menu, button, etc)..
update :
I tried to use character image files from other game that i unzipped, when i run the app, it also looks blurry and jagged. while in the original game, it's so smooth and sharp. why is that ?
check your code, you might use scaledbitmap , see if you can set like this.
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);

Is it possible to get a higher resolution from getDrawingCache?

I am using SubsamplingScaleImageView by Dave Morrissey (https://github.com/davemorrissey/subsampling-scale-image-view) to allow users to crop and pan a photo with gestures.
I modified the library to add a tint and a logo to the photo. Now I need to upload the photo to the server. In order to do that I need to somehow extract the photo from the SubsamplingScaleImageView.
I added the following method to the SubsamplingScaleImageView class:
/**
* Capture a photo of the image view
*/
public Bitmap getOutput() {
buildDrawingCache();
Bitmap b1 = getDrawingCache();
Bitmap b = b1.copy(Config.ARGB_8888, true);
destroyDrawingCache();
return b;
}
I am using this method to get the file, resize it to a specific resolution (800x800), and save it to my app's folder.
The problem I noticed is that the extracted photo from the drawing cache depends on the resolution of the device. For example, on my Full HD device I get 1080x1080 photo, which is enough, but on some lower res devices I get resolutions like 480x480 and that is not enough since the image needs to be bigger than that, so the photo gets blurry when I resize it to 800x800.
Is there a way to get the same resolution photo from that image view on all devices?

Categories