How to access a file in Processing Android Mode - android

I am trying to access a file in Processing Android Mode using the following:
File file = new File(filePath);
And I tried the following options for setting filePath for the file “1.sf2” in the “data” folder that I created in the current sketch directory. However, none of them worked.
filePath = dataPath(“1.sf2”);
filePath = sketchPath(“data/1.sf2”);
filePath = sketchPath(“assets/1.sf2”);
filePath = “data/1.sf2”;
filePath = “assets/1.sf2”;
According to the github page of Processing Android Mode, everying file in the “data” folder of the current sketch directory are automatically copied to the “assets” folder of the generated apk file. However, I keep getting “java.io.FileNotFoundException: /null/restore_pixels: open failed: ENOENT (No such file or directory)” exception every time I run the application on my Android device.
Is there a way to correctly access a file in Processing Android Mode? Thanks a lot!

Related

Android Studio OpenCV yolo where do I put my files?

I am creating an app in android studio that uses library opencv and yolo. I want to store the yolo config file and weights inside the android package. Right now I have those file in the external storage of my phone and I access them like this:
String yoloCfg = Environment.getExternalStorageDirectory() + "/dnns/traffic-yolov3-tiny.cfg" ;
String yoloWeights = Environment.getExternalStorageDirectory() + "/dnns/traffic-yolov3-tiny_15000.weights";
yolo = Dnn.readNetFromDarknet(yoloCfg, yoloWeights);
My question is: where do I put yolo config file and weights for them to be inside the app when the user downloads the apk and how do I get the path to them, since readNetFromDarknet needs as args the path of those files.
I have tried put them in the asset and attempt to get the path to asset but it doesnt work. This is the code I tried:
String yoloCfg = "file:///android_asset/traffic-yolov3-tiny.cfg";
String yoloWeights = "file:///android_asset/traffic-yolov3-tiny_15000.weights";
yolo = Dnn.readNetFromDarknet(yoloCfg, yoloWeights);
This is where I have the files:
Location of files
And this is the error I get:
E/cv::error(): OpenCV(4.0.1) Error: Parsing error (Failed to parse NetParameter file: file:///android_asset/traffic-yolov3-tiny.cfg) in readNetFromDarknet, file /build/master_pack-android/opencv/modules/dnn/src/darknet/darknet_importer.cpp, line 207
E/org.opencv.dnn: dnn::readNetFromDarknet_10() caught cv::Exception: OpenCV(4.0.1) /build/master_pack-android/opencv/modules/dnn/src/darknet/darknet_importer.cpp:207: error: (-212:Parsing error) Failed to parse NetParameter file: file:///android_asset/traffic-yolov3-tiny.cfg in function 'readNetFromDarknet'
E/SurfaceView: Exception configuring surface
I had the exact same issue. What I did to fix this was to add the files in the asset folder; then I created two files and copied the content of the asset folder files there.
Make sure to use the following path and create your files there:
applicationContext.filesDir.absolutePath

Firebase Storage Download getFile(Uri) with "content://" uri throws java.io.IOException: No such file or directory

My use case: Get content from remote only when local uri can't resolve the content.
1) File is available remotely in the Firebase storage with the following reference structure user.uid/files/fileId, example:
ysCwjgc0zGfUNuZimHGaun0MzlP2/files/document-d876e8aa-4b6a-45ed-b2f1-dfd2fbf21360
2) Local device determined the content is not available, attempts to get from Firebase Storage, using the same reference as above.
3) Local creates the file and getUri for the file like below:
// TEST
File f = new File(context.getFilesDir() + "/pictures",fileId + ".jpg");
Log.e(LOG_TAG,"test local file created: " + f.getAbsolutePath());
Uri localUri = FileProvider.getUriForFile(context,FILE_PROVIDER_AUTHORITY,f);
Log.e(LOG_TAG, "test local file content uri created: " + localUri.toString());
4) Tried to use localUri as argument for getFile(android.net.Uri) to get file from Firebase:
but my cat screams the following IO exception.
E/FileRemoteDataSource: Local file not found, trying to fetch from remote...
E/FileRemoteDataSource: test local file created: /data/user/0/com.sample.app/files/pictures/document-d876e8aa-4b6a-45ed-b2f1-dfd2fbf21360.jpg
E/FileRemoteDataSource: test local file uri created: content://com.sample.app.fileprovider/pics/document-d876e8aa-4b6a-45ed-b2f1-dfd2fbf21360.jpg
E/FileDownloadTask: Exception occurred during file write. Aborting.
java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
at java.io.File.createNewFile(File.java:1000)
at com.google.firebase.storage.FileDownloadTask.zza(Unknown Source:75)
at com.google.firebase.storage.FileDownloadTask.run(Unknown Source:190)
5) Tries to use f as argument for getFile(File) to get file from Firebase:
I can get the file perfectly fine, my cat is perfectly asleep with just...
E/FileRemoteDataSource: Local file not found, trying to fetch from remote...
E/FileRemoteDataSource: test local file created: /data/user/0/com.sample.app/files/pictures/document-d876e8aa-4b6a-45ed-b2f1-dfd2fbf21360.jpg
E/FileRemoteDataSource: test local file uri created: content://com.sample.app.fileprovider/pics/document-d876e8aa-4b6a-45ed-b2f1-dfd2fbf21360.jpg
// No exception, got file
Note: In my project I have file providing logic somewhere else, the above just a example case.I would prefer to work with content uri as much as I can, what am I doing wrong by using the content Uri? Thx.
Additional Info:
The Uri points to a file residing in app-internal directory, so storage read/write permissions are and should not be required.
I use Firebase to upload the file by its Uri and it was able to locate, read and upload the file perfectly. The same local uri that was provided by the android framework FileProvider.getUriForFile(f).The test file was uploaded successfully like this:
// putFile(uri) where uri references to a local file where to
// upload the file from.
UploadTask uploadTask = resourceRef.putFile(localUri);
Firebase's getFile also suuports File as an argument, so you may want to try it instead

Ionic: How to get path file in www folder from created plugin

I'm already created a plugin for my project and gratefully its work fine. Then I want to add another function on it that call an image in www folder (www/img/demo.png). I tried with this path
String picPath = "file://android/assets/www/img/demo.jpg";
But, it return with error
java.io.FileNotFoundException (No such file or directory)
Try:
String picPath = "file://android_asset/www/img/demo.jpg;
If you test on android device.

Android: find a file created in AVD

My application has a function of creating an xls file. After that I need to access that file manually. Now, the problem is, when i run the app on my device, I am able to locate the directories and my xls file in internal storage.
But when I try to run that in the AVD, I am not able to find the file. I also viewed file explorer but there also, I wasn't able to find my file.
Where does the file exists when created in AVD?
Below is the code :
File myDir = new File(Environment.getExternalStorageDirectory()+ "/PB/automation/mydir/");
myFolder.mkdirs();
File file = new File(myFolder, "demo.xls");
Trust me, there is nothing wrong with the code.
Below is the file explorer view :

Bad path trying to open file in Android

I'm trying to open a file with this:
document = builder.parse(new File("Data.xml"));
and I'm getting this message:
/Data.xml: open failed: ENOENT (No such file or directory)
and the file is in the root directory of the android project.
You are trying to open a file located in / (in linux this is the root directory of your file system). Instead you should be trying to create a file either on the SDCard or within the local storage directory for your application.
See this for more clarification: http://developer.android.com/guide/topics/data/data-storage.html
Move Data.xml into the assets folder of your project. Then to get a file reference, call getResources().getAssets().openFd( "Data.xml" )
You should probably try using a file input stream constructor for the builder instead, and use openFileInput( String fileName ) to get that, which does only use your app's data directory.
Using persistent storage
openFileInput()

Categories

Resources