Issue
I want to use the function bitmapfactory.decodeFile(string pathname)
.
But on using it, NULL
is being returned.
Please specify what or how to WRITE the PATHNAME, with an example.
for(int j=0;j<3;j++)
{
img_but[count].setImageBitmap(BitmapFactory.decodeFile("/LogoQuiz/res/drawable-hdpi/logo0.bmp"));
linear[i].addView(img_but[count]);
count++;
}
Instead of such a pathname, what should be used?
Solution
If you want to use filenames, then you can not put them inside the drawable folder. The only way you can do this is by putting the images inside the assets folder. If you do not have an assets/
folder, then you must create one inside your main project. It should be in the same branch as your gen/
, res/
, and src/
folders.
You can have any file structure in your assets folder you want. So for example, you can put your images in the assets/images/
folder. Sound files can go in a assets/sounds/
folder. You access an image like so:
public Bitmap getBitmap(Context ctx, String pathNameRelativeToAssetsFolder) {
InputStream bitmapIs = null;
Bitmap bmp = null;
try {
bitmapIs = ctx.getAssets().open(pathNameRelativeToAssetsFolder);
bmp = BitmapFactory.decodeStream(bitmapIs);
} catch (IOException e) {
// Error reading the file
e.printStackTrace();
if(bmp != null) {
bmp.recycle();
bmp = null
}
} finally {
if(bitmapIs != null) {
bitmapIs.close();
}
}
return bmp;
}
The path name, as the variable name suggests, should be relative to the assets/
folder. So if you have the images straight in the folder, then it's simply imageName.png
. If it's in a subfolder, then it's subfolder/imageName.png
.
Note: Android will not choose from density folders in the assets folder. It decodes the images as-is. Any further adjustments for screen density and resolution will have to be done by you.
Opening a File from assets folder in android
Answered By - DeeV
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.