Android openRawResource() not working for a drawable - android

I'm trying to create an input stream by doing this
InputStream is = (InputStream) getResources().openRawResource(R.drawable.image1);
but I'm met with the error "Expected resource of type raw" with respect to my drawable file (R.drawable.image1). image1 is a png and in my res/drawable folder.
Any ideas???

#Broatian I don't currently have a res/raw folder. I found an alternative solution:
is = context.getResources().openRawResource(+ R.drawable.image1);
The + shows additional folders. Thanks for the help!

If you want to open your png with this method then you have to put it in the res/raw folder. Then you'll be able to access the png using R.raw.image1.
Another alternative is to keep it in the drawable folder and then convert the drawable to a InputStream. See How to read drawable bits as InputStream
I assume you know how to get the drawable otherwise look at the Resources documentation.

It is because They are expecting a raw type resource and you are passing a drawable type one.
make a raw folder in the res folder and then put your image in the raw folder.

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

What is the path directory to res/drawable type String to pass through File(String path) in Android?

I am trying to get String path directory to images in the res/drawable and filter through it (jpg, png ..)
Something like this
File folder = new File("/res/drawable");
images = folder.listFiles(IMAGE_FILTER);
File image = images[randGen.nextInt(images.length)];
From sd card works easily but couldn't make it from the resource folder! I don't know if this is naive question but I have been trying different options like passing through uri, casting bitmap to File, no luck!
I am trying to get String path directory to images in the res/drawable
Resources are not files on the device. They are entries in the APK's ZIP file. Hence, you cannot construct a File that points to a resource.
You can use AssetManager to list assets, open InputStreams on assets, etc.

Open an image from asset folder in android

I need to get the images from asset folder by specifying the name of the image and set it to Drawable.
drw = Drawable.createFromStream(getAssets().open("/assets/images/"+drawables[i]),null);
But I get File not found exception.(images is subfolder of assets and drawables[i]- name of the image(say "ball.jpg").
Write instead:
drw = Drawable.createFromStream(getAssets().open("images/"+drawables[i]),null);
file path should be:
"images/"+drawables[i]
when you write getAssets() means open assets folder and find out path there.

what is the path to a drawable image so i can insert in to a file

After failing to insert a sdcard image in to a file, I want to try and insert a drawable image in to a file, anyone know the path to a drawable image? Thx
Make an object of "Bitmap" from the drawble as:
your drawble -> object of BitmapDrawble - > bitmapDrawble.getBitmap() will convert it into Bitmap.
Now write it into a file by converting it into file output stream and write in a file on your SD Card (its a standard code to accomplish this).

How can i get the path of the image which i uploaded to assests or drawable in android?

I am newbie to android don't know much about it. I have imported a image to my project and when i tried it check it using File that the file exists or not.
This is the code i have used..
existingFileName="res/drawable-ldpi/login.png";
File f= new File(existingFileName);
// Log.d("Image path",);
if(f.exists())
{
Log.d("EXISTS", "====File Exists===");
}
Still it is showing me no image exist.
thanks for help in advance,
aby
If you are putting your images in drawable folder, I don't think you can access them like this. Try this
Drawable image= getResources().getDrawable(R.drawable.<id>);
id will be your file name.
If you store the image in the "res/drawable" folder you can do:
Drawable drawable = getResources().getDrawable(R.drawable.name)
See http://developer.android.com/reference/android/content/res/Resources.html#getDrawable
For the case you store your image in the assets folder, you need to use getAssets() ( http://developer.android.com/reference/android/content/Context.html#getAssets() )
You can do something like this:
InputStream is = getAssets().open(imageName);
BufferedInputStream buf = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(buf);
Remember that for both case you need to have a Context (ie an Activity)
There is no need to check the existence of an asset file before you read it. AssetManager takes care of it. If file is missing, it throws an exception. Your coding practice should be to always enclose that part of code in try catch and split the logic. Extra code is not needed. If you try an open a file that doesn't exist you will get an exception.
Though, if you still want to do that manual job, try below code.
AssetManager mg = getResources().getAssets();
try {
//do all considering file exists
mg.open(pathInAssets);
} catch (IOException ex) {
//do all in case file is missing
}
If your file is located in assets/folder/file.ext, then pathInAssets would be "folder/file.ext"
Check for file existence in androids assets folder?
How to check Android Asset resource?
Usually if you use res/drawable, your ressource identifier is compiled into the R.java file. So if you use an identifier from there, you can be sure the file IS there, otherwise you cannot compile your project.
If however you like to exchange R.java later and really need to list the ressources in res/drawable, you can do it with java Reflection, examining the R class like this:
Class<?> c=R.drawable.class;
Field[] fs=c.getFields();
for(Field f: fs)
Log.v("test", f.getName());
You will then get a list of the ressources, without the file extension. The Ressource Identifier (an integer) for every ressource is then read with int id=f.getInt(null);.
Use the DDMS perspective in eclipse to browse for the image on an emulator running your app.
If you get this error.. "Cannot make a static reference to the non-static method getResources() from the type ContextWrapper"
Try Drawable image=Classname.this.getResources().getDrawable(R.drawable.<id>);
here Classname.this means the ClassContext/Application Context.

Categories

Resources