How can I load an image from drawable folder? - android

I've the file name of an image stored in the drawable? how can I open it? I want to know the path?
the file name is String imageFileName = "image.png"
and I want to have the path string something like file://drawable/image.png

You can open it using openRawResource(). See more at the documentation.
See this post regarding how to construct a uri to raw resource, though as far as I know it is not recommended.

You can access it through its resource ID. See previous answers such as this one:
Select Drawable file path

Here is the code segment showing how you can refer to an image through its resource ID.
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.pic1);
int pic_width = mBitmap.width();
int pic_height = mBitmap.height();
See here for more detail

Related

How to read SVG file for ImageView from assets folder Android Studio [duplicate]

I want to show a vector image (say vectorimage.xml) in an imageview from the sd card. Please throw some insight on how to do this in android.
What I have tried already :-
String imagePath = Environment.getExternalStorageDirectory() + "/ folder/productImage.xml";
bitmapImage = BitmapFactory.decodeFile(imagePath);
Drawable bgrImageDrawable = new BitmapDrawable(bitmapImage);
The above code snippet does not work since the bitmapImage is coming as null.
The BitmapFactory can't load vector drawables.
You have to use the VectorDrawable or VectorDrawableCompat class.
To load a vector drawable you need to use a xml loader.
Some parser like the one for the resources need a precompiled binary xml file. You can find them in the apk file when you put the vector drawable in the drawable resource directory.
Here is a sample to load it from the assets, you should be able to use a similar code for the loading from the sd card.
final XmlResourceParser parser = context.getAssets().openXmlResourceParser("assets/folder/image.xml");
drawable = VectorDrawableCompat.createFromXml(context.getResources(), parser);
This way needs at least Android 5.0

Getting String Based Image Location from Drawable Directory

I am trying to use PDFjet library to print a page to PDF in Android.
The documentation tells me that in order to print an image to PDF, I need to get a handle on the image filename like:
String fileName = "images/myimage.png";
So now i want to print an image from the res/drawable folder. In Android the resources are referenced through R.id which returns an int.
My question is:
How do i get a handle on the image from the drawable folder as string ?
try this:
String fileName = "android.resource://your.package.name/" + R.drawable.xxx;;

how to use images present in drawable-hdpi,drawable-mdpi,drawable-ldpi of res folder?

I am able to keep the images of different resolutions inside drawable-hdpi,drawable-mdpi,drawable-ldpi of res folder but do not know how to access the images from assets/www folder . Kindly help.
you need to do any thing more to handle multi-resolution.just get the image by id and system will pic automatically as per current resolution.
http://developer.android.com/guide/practices/screens_support.html#range
You can get an image id by (no matter of dpi):
R.drawable.your_image
So, with id you can do everything that you need (use search).
If you want to use the path try the following :
private void showImage() {
String uri = "drawable/icon";
// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(imageResource);
imageView.setImageDrawable(image);
}
But it's recommended to use the R references :
int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);
Source
drawable-hdpi drawable-mdpi,drawable-ldpi are just different folders to make difference between images quality depending on the screen resolution. The idea is to put the same images in different folder with different resolutions ...
You don't need the AssetManager. You can do
BitmapFactory.decodeFile("file:///android_asset/www/bullet.jpg")
Though storing Images in the Assets is not the best way to go. You will not be able to take advantage of the Android resource management system. So unless you have a compelling reason to do this, I'd suggest that you take a look at using the res folder and the resources system.
Update: Explanation The BitmapFactory has a method to decode a file via the decodeFile method. That's point one. Android lets you access a file in the assets folder via the file:///android_asset/{path} path. In your case, the Image at /image/Malay/bullet.jpg is the assets folder can be accessed via the the file:///android_asset/www/bullet.jpg

Cannot open image file stored in sdcard

In my application I save the contents of a tableLayout as an image in a folder. To allow user to open a file from the saved images I've created a text file that contains the names of these files. These file names will be loaded in an array (files) later. User clicks on "open" to see a list of file names and selects the one he wants to open. I'm using the following code to open a file.
final String imageInSD = extStorageDirectory+"/myFolder/"+files[which];
//where 'files' is an array of strings and contains the names of files.
//and 'which' is the index of the selected element in the list
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView ivv=(ImageView) findViewById(R.id.imageView);
ivv.setImageBitmap(bitmap);
when I try it nothing happens so I tried the following
final String imageInSD = extStorageDirectory+"/myFolder/myFile.PNG";
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView ivv=(ImageView) findViewById(R.id.imageView);
ivv.setImageBitmap(bitmap);
And it shows the image named myFile.
I've already checked if I'm getting the correct file name and path and it seems to be correct. (when i click on myFile.PNG in the list and show the path I get "/mnt/sdcard/myFolder/myFile.PNG").
Why doesn't it work when I use the first code?
String concatenation isn't a good way to combine paths. It is better to use the File constructor :
File directory = new File(extStorageDirectory, "myFolder");
File fileInDirectory = new File(directory, files[which]);
Bitmap bitmap = BitmapFactory.decodeFile(fileInDirectory.getAbsolutePath());

load Image from specific folder on the sdcard?

I'm attempting to create a gallery/gridview that is loaded with images from a specific folder that resides on an SDCard. The path to the folder is known, ("mnt/sdcard/iWallet/Images") , but in the examples I've seen online I am unsure how or where to specify the path to the pictures folder I want to load images from. I have read through dozens of tutorials, even the HelloGridView tutorial at developer.android.com but those tutorials do not teach me what i am seeking.
Every tutorial I have read so far has either:
A) called the images as a Drawable from the /res folder and put them into an array to be loaded, not using the SDCard at all.
B) Accessed all pictures on the SDCard using the MediaStore but not specifying how to set the path to the folder I want to display images form
or
C) Suggested using BitmapFactory, which I haven't the slightest clue how to use.
If I'm going about this in the wrong way, please let me know and direct me toward the proper method to do what I'm trying to do.
my target android sdk version 1.6...
thanks..
You can directly create Bitmaps from decodeFile (String pathName) that will give you Bitmap object that can be set on ImageView
Update: Below is sudo code with minor errors modify it to suit your needs
File path = new File(Environment.getExternalStorageDirectory(),"iWallet/Images");
if(path.exists())
{
String[] fileNames = path.list();
}
for(int i = 0; i < fileNames .length; i++)
{
Bitmap mBitmap = BitmapFactory.decodeFile(path.getPath()+"/"+ fileNames[i]);
///Now set this bitmap on imageview
}
Actually, you are wrong to mention fixed path to access SD-card directory, because in some device it is /mnt/sdcard and in other /sdcard.
so to access root directory of sd-card, use the getExternalStorageDirectory(), it gives you actual path of root directory.
This function will resturn all the files from specific folder you need to pass path till ur folder
public static List getFilesFromDir(File aStartingDir)
{
List result = new ArrayList();
File[] filesAndDirs = aStartingDir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);
Iterator filesIter = filesDirs.iterator();
File file = null;
while ( filesIter.hasNext() ) {
file = (File)filesIter.next();
result.add(file); //always add, even if directory
if (!file.isFile()) {
//must be a directory
//recursive call!
List deeperList = getFileListing(file);
result.addAll(deeperList);
}
}
Collections.sort(result);
return result;
}
BitmapDrawable d = new BitmapDrawable(getResources(), path+".jpg"); // path is ur resultant //image
img.setImageDrawable(d);
Hope it help u...
You can access your directory using File java class, then iterate through all the files in there, create a bitmap for each file using Bitmapfactory.decodeFile() then add the bitmaps to your gallery.

Categories

Resources