Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get file path on raw resources in Android Studio?

Tags:

file

android

I have been using this tutorial to make some face detections on picture. The problem is when I getting the file path that used on java

String xmlFile = "E:/OpenCV/facedetect/lbpcascade_frontalface.xml";
CascadeClassifier classifier = new CascadeClassifier(xmlFile);

How can I translate on android studio. I try put my lbpcascade_frontalface.xml on raw resources. CascadeClassifier is a class that opencv library provided. The only problem is they only loaded string path (on xmlfile). this is my code.

String pathtoRes = getRawPathAtempt2(context);
CascadeClassifier cascadeClassifier = new CascadeClassifier();
cascadeClassifier.load(pathtoRes);

I translated to some method like this.

  public String getRawPathAtempt2(Context context) {
    return "android.resource://" + context.getPackageName() + "/raw/" + "lbpcascade_frontalface.xml";
}

enter image description here

I get the asertions error by opencv that tell me the file is null. Thats mean I had been wrong when I used file path on my method. how can I get file path on raw resources? help me please I have been stuck for several days now

like image 735
Appem Avatar asked Apr 09 '19 10:04

Appem


3 Answers

how can i get file path on raw resources?

You can't. There is no path. It is a file on your development machine. It is not a file on the device. Instead, it is an entry in the APK file that is your app on the device.

If your library supports an InputStream instead of a filesystem path, getResources().openRawResource() on a Context to get an InputStream on your raw resource.

like image 99
CommonsWare Avatar answered Nov 15 '22 09:11

CommonsWare


This is how i solved the problem with @CommonWare Answer

i'm using input stream to get file

is = context.getResources().openRawResource(R.raw.lbpcascade_frontalface);

so make the file will be opened and i will make a point to the File class

 File cascadeDir = context.getDir("cascade", Context.MODE_PRIVATE);
 mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml"); 

and then i make a search to the path file using getAbsolutePath like this

 cascadeClassifier.load(mCascadeFile.getAbsolutePath());

take a look at my full code

try {
        is = context.getResources().openRawResource(R.raw.lbpcascade_frontalface);
        File cascadeDir = context.getDir("cascade", Context.MODE_PRIVATE);
        mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");

        os = new FileOutputStream(mCascadeFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }

        is.close();
        os.close();

        cascadeClassifier.load(mCascadeFile.getAbsolutePath());


    } catch (IOException e) {
        Log.i(TAG, "face cascade not found");
    }
like image 34
Appem Avatar answered Nov 15 '22 08:11

Appem


Uri video = Uri.parse("android.resource://com.test.test/raw/filename");

Using this you can access the file in raw folder, if you want to access the file in asset folder use this URL...

file:///android_asset/filename

Make sure you don't add the extension to the filename. E.g. "/movie" not "/movie.mp4".

Refer to this link:

Raw folder url path?

like image 33
Yogesh Nikam Patil Avatar answered Nov 15 '22 08:11

Yogesh Nikam Patil