I need images to be sorted by folders. like drawable/Photos, drawable/items, drawable/photos. and put each folder to the registry or at least find way to get them out of there. all I want is something close to php like "../img/photos/photo0.jpg". is there way to do something like this. and folder with Images must contain in apk file.
possible solutions is make link in the R file but I didn't find how to do it, other solution is find command with logic like I will show you here:
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageDrawable(Drawable.createFromPath(ImgPath));
OR
ImageView img = (ImageView)CloningTable.findViewById(R.id.img);
String ImgPath = "com.test.test/img/img0.jpg";
img.setImageResource(ImgPath);
please say me the best way to handle it. AND specify if it contain path how I can know path the file lies in.
File ImgFile = new File("test/test.jpg");
TextView testtext = (TextView)CloningTable.findViewById(R.id.testtext);
if (ImgFile.exists()) {
test.setImageBitmap(BitmapFactory.decodeFile(ImgFile.getPath()));
testtext.setText("asdasuidjasdk");
}
can any one say Why programm can't find file and file exist 100% = /?
filepath: Project> assets/test/test.jpg
found solution: Android Show image by path
It's hard to understand why you need to sort resources that will be static in your APK, but you should not access them directly using paths, but using the API for that purpose.
Take a look at the topics in the dev guide here:
http://developer.android.com/guide/topics/resources/accessing-resources.html
http://developer.android.com/guide/topics/resources/drawable-resource.html
Those will teach you how to access the resources. To create Bitmaps from them, the easiest way is to use the BitmapFactory class
Remember, you know which resources the APK has at building time, so you can work around it. If you want to work with Bitmaps created at runtime, then use the data storage methods instead.
In this case, you should put resources in assets folder, and you can access them by path!
So if you want to save the path string, the best way is creating a class, with 2 variants: 1 Bitmap to store data, and 1 String to store the path.
If not, just create a String array that you may access when you want.
In both case, you need to put the path into the Path String storage before you load the image.
Hope I understood your question.
EDIT: Final answer that meet your requirement:
There's no path for drawable, they are all converted into ID. If you put in asset folder, just call it by their name. For example, you have "Test.bmp" in asset folder, its path is "Test.bmp". If you have "SubTest.bmp" in "TestFolder" in asset folder, its path is "TestFolder/SubTest.bmp". Sorry for long time, I had to sleep, it was mid night at my time zone.
Related
At, first i search in Stackoverflow and internet and find many answers but when i try these answers but no answers can solve my problem. in my project i create a directory that name is files.
Picasso.with(MainActivity.this).load("file:///files/img_4.jpg").error(R.drawable.eboss).into(imgArticle);
or
Picasso.with(MainActivity.this).load("/files/img_4.jpg").error(R.drawable.eboss).into(imgArticle);
or
File f = new File("files/img_4");
Picasso.with(MainActivity.this).load(f).error(R.drawable.eboss).into(imgArticle);
or
Picasso.with(MainActivity.this).load("file:/files/img_4.jpg").error(R.drawable.eboss).into(imgArticle);
but nothing work and i only see error image.
You can load image from files too. Picasso allows that.Here is that from Picasso.
RESOURCE LOADING
Resources, assets, files, content providers are all supported as image sources.
Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);
load(new File(...)) - BUT File here must be the ones that are created in /data/data/package.name/...
So either copy file to assets or specify path to /data/data/package.name/... or from sdcard. See this to know how to load from sdcard. You can create a file here using getFilessDir().
I think there is no files/ folder in android project structure. You may like to take a look at Managing Projects Overview.
Picasso will load images from the sdcard on your device, not from your AndroidStudio project. What you could do is to move your picture in the drawable-nodpi folder (create it if it does not exist yet), and then you can load the picture like
Picasso.with(context).load(R.drawable.img_4).error(R.drawable.e;boss).into(imgArticle);
If you want to put the file as part of assets then your directory should be called assets and not files. You can read more here
Based on the comments on your question I think the reason you want to use the "files" folder is because you want to pick the image by name retrieved from the database.
The same is very well possible if the images are still kept in the drawable folder which is generally the case.
You can get the drawable resource using the name picked from the database as follows:
Drawable d= DrawableManager.getDrawable("img_4.png");
This will lead to less load on processor in lookup and reading from storage and less code.
And if you must get the resourse id by the image name use this:
int drawableResourceId = this.getResources().getIdentifier("drawableName", "drawable", this.getPackageName());
and then pass the integer id instead of R.drawable.drawableName
In my app, I've just successfully downloaded a file and I opened it like this:
OutputStream output = openFileOutput("myfile.txt", Context.MODE_PRIVATE);
With a little browsing, I've found my file in /data/data/my.app.namespace/files/myfile.txt. I don't want to hardcode the path but later on in my app I'm going to want to access myfile.txt and I want to dynamically get that folder path. I've tried most of the folders in the Environment object like Environment.getDataDirectory().getAbsolutePath() but that only returns /data. Where can I look to get where private files go? Or is it safe to assume that this structure won't change? I don't like hardcoding it, but it would work...
you could do it by using context.getFilesDir() + "/filename.ext"
I'm stuck in something that I'm sure rather easy
I have a field in the database that I stored the image name in it, ex 'images/ex1.png'
how can I read this into an imageView?? I tried some code in there but can't get it to work
a good example is highly
You cannot set the image file directly as the resource for your ImageView (that only takes the resource ID).
Instead you will have to use a BitmapFactory to decode it and then use setImageBitmap
EDIT :
Your problem is that there is no way for you to map the name of the image file with the resource ID in the code. The easier way is probably for you to put together a map table.
First put your image files in the drawable folder (res/drawable/ex1.png...) and then, do something like this in your code :
Map map = new HashMap();
// Add key/value pairs to the map
map.put("images/ex1.png", new Integer(R.drawable.ex1));
map.put("images/ex2.png", new Integer(R.drawable.ex2));
// do that for all the images....
String image_name = // query to your database
ImageView the_image_to_change = // findViewById(...)
if (map.containsKey(image_name)) {
the_image_to_change.setImageResource((Integer)(map.get(image_name)).intValue()); // not sure so much is necessary...
}
If you're storing this in your /res/ folder, you need to use a ContentProvider in order to get access to this resource. You shouldn't be accessing resources directly unless you're downloading them externally from the app.
Hope this helps.
myImageView.setImageBitmap(BitmapFactory.decodeFile(myFilePathString));
Your issue is that your file path does not include the root of the device's file system. You have to add this to the beginning of your file path string. This depends on how you save the images. So...
To acquire the appropriate file path to a file on the sd card:
File sdCard = Environment.getExternalStorageDirectory();
String filePath = sdCard.getAbsolutePath() + imageFilePath;
If you're saving your images to internal memory, your app is automatically given a folder to save files to and they will be located there. To find the root to that path use getFilesDir(). For more information on data storage check out http://developer.android.com/guide/topics/data/data-storage.html.
Use the res/drawable folder. Here's how to load an image from drawable to a Bitmap.
Bitmap myImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_image);
More on resources: http://developer.android.com/guide/topics/resources/providing-resources.html
I have a web service where I read various information from. One of these pieces of information is the name of an image. I have all the images stored locally in the drawables folder. What I have done right now is I read the string of the image I need from the web service. I want to use this string name and insert the image with the same string name from my drawable folder. Is this possible to do?
Is it possible to do the following somehow?
//Setup the image
ImageView spotIvIcon = (ImageView)findViewById(R.id.spot_selected_image);
String temp = "R.drawable."+spotImage;
spotIvIcon.setImageResource(R.drawable.beer);
What I do with the temp string. Is it possible to convert this string into the int I need? I have the beer in there just to test something out.
Thanks
So I found out how to do this. If anyone wanted to know how it's done use the following code:
int path = getResources().getIdentifier(spotImage, "drawable", "com.androidpeople.tab");
spotIvIcon.setImageResource(path);
Where:
spotImage is the name of your string
drawable is the type you want
com.androidpeople.tab is your package name
Works like a charm now.
you could just use the assets directory to store these images, and then load them using something like BitmapFactory.decodeStream(assetManager.open(imAnAssetName));
the asset directory it's here to be used for things you don't want to be managed (or that can't be managed) by the resource manager.
http://developer.android.com/reference/android/content/res/AssetManager.html .
I want to load bitmaps into ImageViews in Android, but I don't want to use the R.drawable syntax because I have a lot of images with a standard naming convention so it's much easier to get the image with some logic. For example, all my images are named:
img1.png
img2.png
img3.png
So if a user selects a value, let's say x, I show "img" + x + ".png" in the ImageView. Looks like Bitmap.decodeFile is what I need, but I need to know the syntax of how to get to my drawables folder since that's where the images are. Or if there's perhaps a better way.
I realize I could do this with a switch statement instead of concatenating the image name but that would be a lot of lines since I have so many images.
One alternative is to use reflection to load images from R. You can looking static variables by string name. Keep in mind that reflection can be slow though. If you are going to be loading the same image multiple times, you might want to keep a cache of name -> R id.
Edit:
You can also access them by URI. See referring to android resources using uris.
The R.drawable value is a number by itself. You just need to use the first image id and add a fixed value to find the right image. For example:
R.drawable.image1 = 1234;
R.drawable.image2 = 1235;
etc.
So to get image 2 I would to R.drawable.image1 + 1.
Every time the R.java file is regenerated the numbers may change but the sequence will be the same. If you don't want to depend in this then you will have to look at the "assets" folder.
Looks like I've found a solution. The real problem is that I generate the resource names at runtime, but I can get the resource id like this:
getResources().getIdentifier("string1" + string2, "id", "com.company.package")
I can then pass that value to setImageResource or any other method that accepts a resource id.