I'm working with a library and I need to get a file path stored at raw folder, so library methods create a FileInputStream, but I always get a FileNotFoundException from library methods.
So I have created a class and try:
String path = "android.resource://" +context.getPackageName () + "/" + "myFileName";
InputStream fis2 = getResources().openRawResource(R.raw.myFileName);
File f = new File(path);
fis2 and f are correctly created and I get no FileNotFoundException.
But when I try: FileInputStream fis = new FileInputStream(path); I get the FileNotFoundException.
I need to get a file path stored at raw folder
That is not possible, as it is not a file.
so library methods create a FileInputStream
Those library methods need to be adjusted to work with an ordinary InputStream. Then, use your fis2 InputStream.
Related
I have a FileDescriptor open (credits to Andrew) in Android Studio and i want to write it to a new file (copy), how can i do that?
You can do it using Files.copy method the following way:
FileInputStream input = new FileInputStream(descriptor);
Path path = Paths.get(outDirectoryName, outFileName);
Files.copy(input, path, StandardCopyOption.REPLACE_EXISTING);
input.close();
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";
}
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
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.
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");
}
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?
We can use this Alternative method for finding the path of Files in Raw Folder.
InputStream inputStreamdat=getResources().openRawResource(R.raw.face_landmark_model);
File model=getDir("model", Context.MODE_PRIVATE);
File modelFile=new
File(model,"face_landmark_model.dat");
FileOutputStream os1 = new
FileOutputStream(modelFile);
byte[] buffer1 = new byte[4096];
int bytesRead1;
while ((bytesRead1=inputStreamdat.read(buffer1))!=-1) {
os1.write(buffer1, 0, bytesRead1);
}
inputStreamdat.close();
os1.close();
You Can Use
modelFile.getAbsolutePath()); For getting the Path
Android Studio 0.8.1
Hello,
I have created a keystore called snapzkeystore.bks using the keytool. I want to get the path so I can pass it to a function and load it. However, I can’t seem to get the path. I have also tried putting it in the raw and assets folder and trying to get it that way and testing what works, However, I also get a FileNotFoundException calling this function keyStoreFile.load(fileInputStream, password) (code below)
Trying to get it when put in the raw folder Test 1:
String uri = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.snapzkeystore;
snapClient.makeRequest(uri.toString());
I get FileNotFoundException Exception and the and the string contains:
android.resource://com.sunsystem.snapzui/2131034112
I have also tried using the assets folder and using this instead Test 2:
Resources resources = getResources();
InputStream inputStream;
try {
inputStream = resources.getAssets().open("snapzkeystore.bks");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(buffer);
byteArrayOutputStream.close();
inputStream.close();
Log.d(TAG, "asset: " + inputStream.toString());
snapClient.makeRequest(inputStream.toString());
} catch (IOException e) {
Log.d(TAG, "IOException: " + e.getMessage());
}
I get FileNotFoundException Exception and the and the string contains:
android.content.res.AssetManager$AssetInputStream#5363f738
Source snippet for loading the keystore:
public int makeRequest(String keyStoreFileName) {
try {
KeyStore keyStoreFile = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream fileInputStream = new FileInputStream(keyStoreFileName);
// FileNotFoundException
keyStoreFile.load(fileInputStream, password);
Just a quick questions, which is the best folder to put a keystore in, raw or assets?
Many thanks in advance for any suggestions
I want to get the path so I can pass it to a function and load it. However, I can’t seem to get the path
That is because there is no path. Those are not files. They are entries in the APK file.
I get FileNotFoundException Exception and the and the string contains: android.content.res.AssetManager$AssetInputStream#5363f738
That is because you called toString() on an InputStream returned from the AssetManager.
Source snippet for loading the keystore
load() takes an InputStream. You do not have to use a FileInputStream. You are welcome to pass the InputStream that you get from the AssetManager to load():
KeyStore keyStoreFile = KeyStore.getInstance(KeyStore.getDefaultType());
keyStoreFile.load(resources.getAssets().open("snapzkeystore.bks"), password);
Just a quick questions, which is the best folder to put a keystore in, raw or assets?
Either should be fine. You can use openRawResource() on Resources, IIRC, to get an InputStream on a raw resource.
I'm writing to a file using the code below:
File file = new File(getCacheDir(), "cachefile");
FileOutputStream fos = new FileOutputStream(file);
StringBuilder cachetext = new StringBuilder();
Iterator bri = brands.iterator();
Iterator bli = brand_id.iterator();
while(bli.hasNext()) {
cachetext.append(bli.next() + "|" + bri.next() + System.getProperty("line.separator"));
}
fos.write(cachetext.toString().getBytes());
fos.close();
This works fine - no errors and the file ends up containing what I expect it to contain. When I go to read it via openFileInput(), however, I get an exception telling me that path separators are not allowed
FileInputStream fis = openFileInput(getCacheDir() + "/cachefile");
Fair enough, that contains a slash, but how else can I specify the path of the file I want to open? There must be a way to do this, of course, but I can't find answers via Google ('read', 'cache' and 'file' not being the most niche of terms ...) so I thought I'd try the human touch. Thanks in advance!
you do it pretty much the same way you created the output file:
FileInputStream fis = new FileInputStream(new File(getCacheDir(), "cachefile"));
I am new to android and I want to read a binary file extension .AMF file.
I really need your help this is really urgent.
Thanks in advance.
I made a folder in RES named raw. Here is the code I tried, it says file not found.
FileInputStream in = new FileInputStream("R.raw.hello.txt");
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader inRd = new BufferedReader(isr);
String text;
while ((text = inRd.readLine()) != null) {
inLine.append(text);
inLine.append("\n");
}
in.close();
return inLine.toString();
From the Andoid SDK:
To read a file from internal storage:
Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.
Read bytes from the file with read().
Then close the stream with close().
Tip: If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
InputStream input = getResources().openRawResource(R.raw.hello.txt);