Bitmap and proper saving location - java

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.

Related

How to get image from notification?

How to get an image from notification? I'm using NotificationListenerService.
I found the way to get a small icon and large icon. but I don't know how to get an image.
(ex. Whenever someone sent me the message text with image)
I tried
Bitmap temp = (Bitmap) extras.get(Notification.EXTRA_PICTURE);
However "temp" is null
Help me please!

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.

Display images from Drawable folder using Universal Image Loader

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);

Take Screenshot of Android screen and save to SD card

There are a few questions here on SO about capturing screenshots of an android application. However, I haven't found a solid solution on how to take a screenshot programatically using the android SDK or any other method.
So I thought I would ask this question again in the hopes that I can find a good solution, hopefully one that will allow capturing full length images that I can save to the SD card or somewhere similar.
I appreicate any help
This is not possible directly on the device/emulator, unless it is rooted.
to honest all I need it for is the emulator as this is for a testing application on a PC
This sounds like a job for monkeyrunner.
monkeyrunner tool can do the job for you with bit of adb command, [python script]
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
//waits for connection
device = MonkeyRunner.waitForConnection()
//take the current snapshot
device.takeSnapshot()
//stores the current snapshot in current dir in pc
device.writeToFile('current.png')\
//copy it to the sd card of device
os.subprocess.call('adb push current.png /sdcard/android/com.test.myapp/current.png')
Note: call this jython script file
monkeyrunner.bat <file name>
You will most likely not be happy with this answer, but the only ones that I have seen involve using native code, or executing native commands.
Edit:
I hadn't seen this one before. Have you tried it?:
http://code.google.com/p/android-screenshot-library/
Edit2: Checked that library, and it also is a bad solution. Requires that you start the service from a pc. So my initial answer still holds :)
Edit3: You should be able to save a view as an image by doing something similar to this. You might need to tweek it a bit so that you get the width/height of the view. (I'm inflating layouts, and specify the width/height when I layout the code)
View content = getView();
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
File file = new File(pathAndFilename);
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
You can look at http://codaset.com/jens-riboe/droidatscreen/wiki (with a write up at http://blog.ribomation.com/2010/01/droidscreen/): this is a Java library that uses adb to capture a screen shots. I've been able to (with a lot of elbow grease) modify the source to let me automatically capture a timed series of screen shots (which I use for demo videos).
You can see the class structure at http://pastebin.com/hX5rQsSR
EDIT: You'd invoke it (after bundling all the requirements) like this:
java -cp DroidScreen.jar --adb "" --device "" --prefix "" --interval

Categories