I returned a string from SQLite table and want to load image from the drawable folder. Is it possible?
For example :
String name = sql.getName(1); //name is now Barney
img.setImageResource(R.drawable.name);
Is it possible to load it this way?
I've tried.
int imageid = getResources().getIdentifier("com.tes.test.main:drawable/" + name.toLowerCase(), null, null);
img.setImageResource(imageid);
Doesn't work either. :(
Any help is appreciated. Thanks.
1st. A few weeks ago i used this for a VideoView. Must be nearly the same for pictures.
In my place i stored the videos in the /raw folder.
mVideoView.setVideoPath("android.resource://your-package-name/raw/"+videoName);
2nd. Why don't you save the picture into the database and read it from there?
like this:
byte[] blob = cursor.getBlob(2);
Ah, I've found it.
String image = "test";
int pimageid = getResources().getIdentifier("com.yourpackage.name:drawable/"+image,null,null);
img.setImageResource(pimageid);
Related
I am reading data of employees from excel file stored in internal storage, unfortunately the app is totally offline and so have offline images , which i need to store in drawable folder.
Now the question i have is ;
There are 350 images named as employee id , for example
1.jpg of emp id = 1 , 2.jpg of emp id = 2.
I am using RecyclerView , in that onBindViewHolder method i have to set images according to the employee id.
Please guide me to show the image into imageView.
Please comment for more input from my side.
Plz dont mark it negative if you can't answer.
You could try something like this.
Example if trying to get employee 1526 the drawable would be R.drawable.1526
String id = employee.getId(); //1526
int imageId = context.getResources()
.getIdentifier(id, "drawable", context.getPackageName());
id being a string.
then with the image view
imageView.setImageResource(imageId);
is it possible to avoid storing images in a database and putting them into the drawable or mipmap folders instead? In the database then I would like to put only the reference to ressources and call it like this:
String image = "R.mipmap.image1"; //This will be the data retrieved from Database
int imageInt = Integer.parseInt(image);
imgview.setImageRessource(imageInt);
Actually I want to use this in a RecyclerView so I simplified the example code here but this is showing me the following error
java.lang.NumberFormatException: Invalid int: "R.mipmap.image1"
Thanks
One simple way is to store the image name in database (i.e. "image1" in your example). Then later get the identifier for it using image name as below:
int imageId=context.getResources().getIdentifier("imageName","mipmap",context.getPackageName());
Now, you can use this imageId for setting image to image view.
imgview.setImageRessource(imageId);
I am working on an Android app like Notepad.
There are two sections.
Text Only
Text with images
A notepad which can only get text from user and save it in db and then retrieve is done.
The another section, will work basically like MS Word.
User can select image from gallery and save it in notepad.
But how can I achieve it? Because I want to store that whole data (image & text) into db as it is. Means suppose user added an image after 2 lines of text. Then app should retrieve it from db in that format only.
Any suggestions? Please help me. Thanks in advance.
The most simple solution would be to define tags or some other form of markup language, that would store that image within a text as a path to file on Disk for example which is imho better alternative than storing it in SQLite but if you insist on database, you can store something like an imageId and keep it the there... However, you are manually doing something, that has already been optimized for you when trying to handle the byte array instead of a file in memory ..
Once you store images in this custom markup language, you can parse it and split the text on chunks between the images which you populate dynamically to some ViewGroup(i.e. LinearLayout) as separate TextViews and ImageViews...
Finally, if you want a solution quickly, you can use some already developed libraries such as this : https://android-arsenal.com/details/1/1696 ... However, this pattern is so common that you can find a great number of libraries that do exactly the thing you are looking for ...
Don't reinvent the wheel if it's not necessary..
You can save the byteArray or uri of images in the database.
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray();
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
Uri uri = Uri.parse(path);
Hope this helps ....
Currently I can pass the path to an image in the sdcard. Now I want to pass a path to a folder(to the same function BitmapFactory.decodeFile(path)) so that all the images in the folder can be displayed using
image.setImageBitmap(img);
Basically, I want to display all the images in the selected folder as a slideshow, one after the other. Any help / pointers on this?
It's hard to tell exactly what you're asking for here. You've asked a very general question. I think what would be best for you is to use a Gallery widget.
The first step is getting URLs or paths to all the images. I can think of two alternatives.
1) List all files in whatever folder you are looking for and get the ones of the right type. You can use listFiles to get all the files in a folder:
File files[] = Environment.getExternalStorageDirectory().listFiles();
and string split or substring to get the extension
string filename = file[i].getName();
int pos = filename.lastIndexOf(".");
string ext = filename.substring(pos);
2) You can use a content resolver and get the images straight from the Android MediaStore. This solution would be more robust. Search for how to use the MediaStore if you want to use this.
Second step is to use BitmapFactory to get all the bitmaps you need. Yes, you will need to call decodeResource once for each bitmap you get from a file.
I've searched Google and this website many times, but can not find a solution to my question.
First, I understand, you can load images using something like:
int image = R.drawable.icon; (assuming there's a file called 'icon.png')
and I've read about, and tried using:
getResources().getIdentifier("resname", "restype", com.example.whatever);
But, here's what I'm unable to do:
I want to be able to have pictures in the /res/drawable folder (I believe that's the correct folder), WITHOUT KNOWING ANY OF THE NAMES OF THE FILES, and load them dynamically - at run-time.
One (of the many) things I've tried is (something like):
int[] images = new int[numberOfImages];
for( 0 to numberOfImages )
{
images[i] =
getResources().getIdentifier("resname", "restype", com.example.whatever);
}
This returns 0 EVERY TIME, so it's not going to work
I'd like to get the name and integer identifier for every picture in the /res/drawables folder. Can this be done WITHOUT KNOWING ANY FILE NAMES?
----------------------------------------------------------------------
[adding this after the question was resolved to help anyone that may run into the same issue in the future. It just shows that it does in fact work.]
Class resources = R.drawable.class;
Field[] fields = resources.getFields();
String[] imageName = new String[fields.length];
int index = 0;
for( Field field : fields )
{
imageName[index] = field.getName();
index++;
}
int result =
getResources().getIdentifier(imageName[10], "drawable", "com.example.name");
Well, if you have to do it, use reflection :)
Class resources = R.drawable.class;
Field[] fields = resources.getFields();
for (Field field : fields) {
System.out.println(field.getName());
}
However, in a way, you are still hard-coding stuff, since you will only be putting a fixed set of drawables in your app. :)
As you stated in the comments, if you want to add files to that folder after the app is installed you'll have to use a private folder. Data storage documentation.
Then use a service to download images and add them to the private folder.
If you want the user to download the app with some preloaded images and you don't want to wait for the service run for the time to download the initial set of images, you can always send some images in the /res/raw folder and in the first time your app runs, copy these to your private folder.