4

I have written an android OpenCV application and for testing my functions, I need to load some sample images from my computer and save the results back to my computer to see how if the functions work correctly. I put a jpg file under resources folder in Android Studio and I could load a file from this folder but BitmapFactory always returns null. how should I solve this issue?

File file = getFileFromPath(this, "roi_RGB.jpg");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inSampleSize = 4;
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options); 

I load the images as :

private static File getFileFromPath(Object obj, String fileName) {
    ClassLoader classLoader = obj.getClass().getClassLoader();
    URL resource = classLoader.getResource(fileName);
    return new File(resource.getPath());
}

bitmap is always null.

hAlE
  • 1,283
  • 3
  • 13
  • 19

2 Answers2

1

You cannot reference desktop file in android because it cannot read from desktop. Take a look in this answer on how to use a test resource folder instead.

Community
  • 1
  • 1
Joshua
  • 5,901
  • 2
  • 32
  • 52
  • I have tried putting my images in resources folder and loading it. (edited in my post). I still get a null bitmap – hAlE Jun 24 '16 at 01:28
0

The test should be written in the AndroidTest (instrumented unit tests). Apparently the Bitmap.decodeStream or Bitmap.decodeFile do not work in local unit tests.

iceon
  • 1
  • 1