Display images from Drawable folder using Universal Image Loader - java

I want to load image from Drawable folder using Universal Image Loader (NOSTRA my code is below
imgLoader = ImageLoader.getInstance();
imgLoader.init(new ImageLoaderConfiguration.Builder(this).build());
Now i am loading image in wallImage (ImageView) below is my code
imgLoader.displayImage("drawable://" + result, wallImage);
my problem is, it is taking to much time to load image. Can anybody give me solution what should I have to do ?

Imgloader.displayImage("drawable://" + result, wallImage);
this method is not recommending for loading the images in drawable... You should use native method as Vigbyor as suggested
imageview.setImageResources(res id);

Related

Bitmap and proper saving location

Hi everybody I've got the following problem with an adroid app: I want to read an image saved and perform some operations on it.
(I do not need to use this image to draw something on screen, I just have to check the color of some pixels)
I'm using the following code:
Bitmap bitmap = BitmapFactory.decodeFile("drawable-v24/ean13.bmp");
The fact is that the BitmapFactory returns null as a FileNotFoundException is thrown. (by now the file is saved inside res folder)
I don't really understand where I should put the image and how to give the path to the BitmapFactory to be able to get it properly.
(I guess the problem is due to the fact that the image actually is stored on my pc and not on the Android device but I can't understand how should I proceed)
Thank you in advance for the help! :)
If you are accessing image from resource drawable of mipmap. You should use BitmapFactory.decodeResource to get Bitmap.
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
Or another overloaded version .
public static Bitmap decodeResource(Resources res, int id, Options opts)
where getResources() is method of Activity.

Android - I am having problems with adding more than 8 imageviews to RelativeLayout

enter ?
I need to add 24 images to RelativeLayout and I first tried it with XML,
which just shut down the app after I added 8th ImageView.
So I tried with coding like above, and it shows me that message.
It says OutOfMemory, failed to allocate ___ to a memory with 6mb OOM and I don't really know what it means but I am guessing image files are too large?
It caused error like that when I just tried with XML too.
enter image description here
but the entire set of images are less than 100kb though.
How can I deal with this issue?
looks like you have drawables in drawable folder. So if you have really large-density screen, it scales. You should create scaled images for all dpi's or init images manually:
Options options = new Options();
options.inScaled = false;
bitmapImage = BitmapFactory.decodeResource(context.getResources(),
R.drawable.imageName, options);
imageview.setImageBitmap(bitmapImage);
or
InputStream is = this.getResources().openRawResource(imageId);
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
imageview.setImageBitmap(originalBitmap);
Use Picssso library (http://square.github.io/picasso/). It has fit() method that is very useful for loading many images. Example:
Picssso.with(context).load(R.drawable.yourimage).fit().into(yourImageView);

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.

How can we convert a Bitmap into an image OR store into a Variable?

I am making an android application in which I am showing image from internet into a ImageView by help of these classes
MemoryCache
ImageLoader
FileCache
Utils
In my main class I am calling this method imgLoader.DisplayImage(strURL, loader, image); to show the image and It is working really very fine and I think The four classes which I have mentioned above are common may be people know about that (If you don't then here's the code)
Now I want this method to call in AsyncTask class method doInBackground()
how can I do this.
And here is what I am doing is:
protected ImageView doInBackground(String...args)
{
try
{
imgLoader.DisplayImage(strURL, loader, image);
}catch(Exception e)
{
e.printStackTrace();
} ;
return image;
}
I know that it is completely wrong we can not pass an image as ImageView please help me how can I return the image from this function.
if you're going to be loading an image in a background thread via an async, just store the image url to a string and update your views in the onPostExecute(void..) function
Honestly though, I wouldn't use imageLoader for anything. I've used it before and it has limitations. Picasso is a much better way to cache and load images. Faster as well.
http://square.github.io/picasso/
Lmk if you have anymore questions!

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