How to write a FileDescriptor to a new file (Android) - android

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();

Related

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

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

How to append to a file using android Storage access framework?

How can I use storage access framework to get a FileOutputStream to append to a file? The following code overwrites the file
void write(Uri uri){
try {
ParcelFileDescriptor descriptor=getContentResolver().openFileDescriptor(uri,"w");
if(descriptor!=null) {
FileOutputStream op=new FileOutputStream(descriptor.getFileDescriptor());
op.write("test string".getBytes());
op.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
I know doing it with java.io.File using FileOutputStream(File file,boolean append)
But I can't see FileOutputStream(FileDescriptor fd,boolean append)
If you want to append to the file, you need to use access mode "wa" instead of "w" in this call:
ParcelFileDescriptor descriptor =
getContentResolver().openFileDescriptor(uri, "w");
Instead of creating FileOutputStream from
FileOutputStream op = new FileOutputStream(descriptor.getFileDescriptor());
you can directly get OutputStream using
context.getContentResolver().openOutputStream(yourUri, "wa");
and provide this following modes w wa rw or rwt
and to append existing file use wa

FileNotFoundException: /storage/emulated/0/Android

I try this file writer/reader code segment for test:
File file = new File(Environment.getExternalStorageDirectory(), "LM/lm_lisdat_01.txt");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(("test").getBytes());
outputStream.close();
File file = new File(getExternalFilesDir(null), "LM/lm_lisdat_01.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
In the 4. row i got this error message below but the "lm_lisdat_01.txt" file was created in LM directory:
java.io.FileNotFoundException: /storage/emulated/0/Android/data/hu.abisoft.lm/files/LM/lm_lisdat_01.txt: open failed: ENOENT (No such file or directory)
Can help anyone for answer this (i think simple) question? I'm newby in Android. Thank you!
You are creating the file in one directory and trying to open it for input in another.
Environment.getExternalStorageDirectory() is /storage/emulated/0
getExternalFilesDir(null) is /storage/emulated/0/Android/data/hu.abisoft.lm/files
Use the same directory for file creation and input.
With sdk, you can't write to the root of internal storage. This cause your error.
Edit :
Based on your code, to use internal storage with sdk:
final File dir = new File(context.getFilesDir() + "/nfs/guille/groce/users/nicholsk/workspace3/SQLTest");
dir.mkdirs(); //create folders where write files
final File file = new File(dir, "BlockForTest.txt");
Please see changes. Your path was wrong.
And also check whether file exists or not.
File file = new File(Environment.getExternalStorageDirectory(), "LM/lm_lisdat_01.txt");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(("test").getBytes());
outputStream.close();
File file = new File(Environment.getExternalStorageDirectory(), "LM/lm_lisdat_01.txt");//changes here
if(file.exists())
{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
}

FileInputStream FileNotFoundException

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.

how to read binary .AMF file in android

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);

Categories

Resources