Can't load image from disk with picasso - android

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

Related

Can there be a content directory, where I can put files (compile time) and load files from (run time), inside an app?

I have some image files which will be dynamically loaded in my app. I know I can put images in the "/res/drawable" directory, but can there be somewhere else I can put files and loaded them by file name at runtime?
The image files contain characters that are now allowed in resource names. I did not name them, so ideally, I need to keep the names. For example, suppose I have "Cat #1.png" and "Cat #2.png", and I would like to load the image into an ImageView by that name at runtime, like so:
MyImageView1.setImageBitmap(createBitmapFromContentDirectory("Cat Pictures/Cat #2.png"))
MyImageView2.setImageBitmap(createBitmapFromContentDirectory("Dog Pictures/Dog #1.png"))
Is that possible, or should I change all image names into conforming names (e.g., cat_number1.png and cat_number2.png) and put them into the drawable directory?
You can put them in assets & load them from there,
Otherwise you can also put them in raw folder.
you can refer to Where do I I Place assets folder for assets.assets doesn't create Resource Id, so you can access them directly. Have a look at this
difference between assets & raw.

keeping images in a categorized way in android drawable folder and how to access them

I have almost 1000 images with different categories like
Students_Images_Folder
Teachers_Images_Folder
Parents_Images_Folder
etc.
I want to keep import these images in my android project with categories not mdpi, xdpi,hdpi etc . Is there anyway to keep them categorized not merged in drawable.
Should I go for database ? I read that database is not suitable as it will lower efficiency. Any solution is welcomed. thanks for giving time to my silly question.
First of All you should add all images into drawable folder with unique name and of course some prefix like st for Student, tr for teacher and pr for parent.
Secondly you can create database with three tables to store the names of images into it individual category wise.
After that you can retrieve the images category wise from database and here is code to deal with resource id from file name.
OR
You can also create assets folder in android, here you can see how to create assets folder in android studio.
After creating assets folder you can create sub-directories according to your categories like student, teacher and parent.
And after than put images inside those directories.
Lastly load images from assets. To load images from sub-directories of assets implement following code
try
{
InputStream ims = getAssets().open("student/st_img.jpg");
Drawable d = Drawable.createFromStream(ims, null);
mImage.setImageDrawable(d);
ims .close();
}
catch(IOException ex)
{
return;
}
You can make subdirectories in your resource directory and make your build system use it with a small piece of Gradle script.
Read here for more information: Can the Android Layout folder contain subfolders?
Or this blog here: Android: Multiple Resource Folders
AFAIK, you can't add subdirectories to res folders, this will cause the resources compiler to fail!
But you can add a prefix at the beginning of imgaes's names for each category, for example: st_image_name for students images

Where to store many subofolder locally images android

I have a series of images (200-300) of animals that will be loaded inside my application.There is a root folder and subfolders containing 3-4 images to any type of animal.
The application must work in local.Where I recommended to save these pictures? In assets or drawable directory ? Or something else ?
Thanks in advance
You should store your images in drawable folder.
You can create sub folders in assets dir. and call it with reference to the folder / dir structure.
I'm not sure though you can do it with drawables or not as never tried it but you can give it a try. Rest follow the below arrays approach to manage your code better.
Plus if you want to categorize them then care different arrays for each category and put the resource reference in each one of them accordingly. Then you can user these category arrays according to your needs.
e.g
String dogs[] = {R.drawable.dog1, R.drawable.dog2, R.drawable.dog3, etc};
String cats[] = {R.drawable.cat1, R.drawable.cat2, R.drawable.cat3, etc};
and you can use these arrays in their own category.

reading image path from a database

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

Sorting Images on Android

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.

Categories

Resources