InputStream caInput = new BufferedInputStream(new FileInputStream("cert.der"));
09-10 20:59:02.526: I/System.out(11341): ssdada cert.der: open failed: ENOENT (No such file or directory)
I am using android with eclipse. I have cert.der on application level, under my src folder..and under the same folder as well as my Class that am calling the above code from.
Where shall I add it still?
You should put it in the raw or assets. Documentation.
raw/
Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.
However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.
Related
I have a temp file that is made for creating an image from a cropping library and I can see the file in Device File Explorer but when I try to open the file I get this error:
java.io.FileNotFoundException: file:/data/user/0/com.example.demo/cache/.tmp/cropped1651879842159823361.png: open failed: ENOENT (No such file or directory)
This is how that file is created:
val croppedImageFile = File.createTempFile("cropped", ".png", viewModel.tempPath)
val destinationUri = Uri.fromFile(croppedImageFile)
viewModel.tempPath is just the following:
viewModel.tempPath = "${this.cacheDir}/.tmp"
I can see that file got created and is valid, but when I try to access it, it claims it doesn't exists. I simply open the file by doing File(uri.toString()). in the view model
I'm not sure what is wrong and why it can't find the file. If this matters, I'm using an emulator that has google play and it's Android 11.
You need to open the file using new File(uri.getPath()).
uri.toString() returns the URI as a string, that means "file://path/to/file" which is not a valid path.
I have an existing .json file in my assets folder. I can read successfully using Input steam but when I want to write to this file I'm having trouble finding it correctly. Here's how I have it now
try (FileWriter file =
new FileWriter(getApplicationContext().getPackageName() +
"/app/src/main/assets/inventory.json")) {
file.write(jsonObj.toString());
}
Here's the error I'm getting
01-09 14:43:51.178 21391-21391/edu.wit.senderp.inventorytrack W/System.err: java.io.FileNotFoundException: edu.wit.senderp.inventorytrack/app/src/main/assets/inventory.json (No such file or directory
Files in assets folder are read-only. And you can only access assets files via AssetManager.
https://developer.android.com/guide/topics/resources/providing-resources#ResourceTypes
I'm trying to append a json object to my json array. So for i think it got it. But i'm trying to get the file file location on my json file. It's on my assets folder. here's the code and the error. How can i get the file location on my assets folder?
File fileJson = new File(getApplicationContext().getExternalFilesDir("/app"), "products.json");
03-04 15:26:33.609 6784-6784/com.elishanarida.json W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.elishanarida.json/files/app/products.json (No such file or directory)
Assets file only read and can't be wrote, you can get assets file by AssetsManager.Assets file is different from ExternalFilesDir,it is in your apk rather than the SD memery.So new File(getApplicationContext().getExternalFilesDir("/app"), "products.json");is wrong.
Is there a way to open assets or raw resource as File ? I have method that needs open file as File but since i want store my file in assets or in raw folder i cannot access them.
I tried open it with file:///android_asset/filename.xml but received FileNotFound exception.
File file = new File("....");
x.load(file);
Is there a way to open assets or raw resource as File ?
No, sorry. They do not exist as files. You can only get an InputStream.
If you're willing to zip up the file first, you can do this:
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.texfilezip), dir, true);
File textFile = new File(dir, "textfile.txt");
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()