0

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?

juan.facorro
  • 9,791
  • 2
  • 33
  • 41
Vaido
  • 111
  • 1
  • 7
  • You can't specify a pathname in the res folder like that. Use decodeResource(getResources(), R.drawable.logo0); – DeeV Jun 26 '13 at 19:22
  • Wad is I want to only use the decodeFile(String pathName) ? Please help with Wad the PathName stands for !.... I have 300+ images, I want to use a string pathname to concatenate with the loop variable n decrease the length of my code ... – Vaido Jun 27 '13 at 16:46

1 Answers1

1

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

Community
  • 1
  • 1
DeeV
  • 35,865
  • 9
  • 108
  • 95