My goal is very simple: I have a zip file in my assets folder and wish to get the last modified date of it.
I am able to access the file via the Assetmanager and create a file from it, but the modified date is the moment that file is written
AssetManager manager;
File checkFile = new File(source + "/" + "test.zip");
try
{
InputStream stream = manager.open(fileToCheck);
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
FileOutputStream fos = new FileOutputStream(checkFile);
fos.write(buffer);
fos.close();
}
catch(IOException ioExc)
{
//stuff
}
long lastMod = checkFile.lastModified();
Date lastMod = new Date(lastMod ); //This is returning the current time,
//NOT the modified date of the file
Can anyone think of a way I could access the actual last modified date of the file in the assets folder? Any help would be greatly appreciated!
The easiest way is you can include the last modified date into that zip file (e.g, lastmod.txt). You can then unzip and read that file.
Related
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
I would like to store my values as a textfile and hence i used
try
{
File Mydir = new File("/sdcard/app/");
Mydir.mkdirs();
File outputFile = new File(Mydir, "helloworld");
FileOutputStream fos = new FileOutputStream(outputFile);
fos.write(item1.getBytes());
fos.write(item2.getBytes());
// Close output stream
fos.flush();
fos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
This is working extremely well and the values are storing properly. But now my question is i would like to store large number of values in a single file and i would like to store those values in the table format. Here the values are getting over writted one above and the last used values is only displaying. So instead of this i would like to store all the used values in a table format.
I didn't understood the requirement of writing in tabular format but if you simply want that the new text don't overwrite the existing text in file , or simple append the new text in file , you should use
FileOutputStream fos = new FileOutputStream(outputFile,true);
instead of
FileOutputStream fos = new FileOutputStream(outputFile);
i.e.
try
{
File Mydir = new File("/sdcard/app/");
Mydir.mkdirs();
File outputFile = new File(Mydir, "helloworld");
FileOutputStream fos = new FileOutputStream(outputFile, true);
fos.write(item1.getBytes());
fos.write(item2.getBytes());
// Close output stream
fos.flush();
fos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
Edit
For current system Date and time
SimpleDateFormat ft =new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss:S a zzz");
Date date= new Date();
String nowTime = ft.format(date);
concatenate nowTime with item1 and item2 before writing it on the file
I am trying to save a p12 file to internal storage like so:
File file = new File(mContext.getFilesDir(), "fileName.p12");
I then proceed to read this file into a byte array like so:
byte[] bFile = new byte[(int) file.length()];
try {
bFile = org.apache.commons.io.FileUtils.readFileToByteArray(file);
}catch(Exception e){
e.printStackTrace();
}
After doing this, I get a FileNotFoundException even though the debugger shows that the file is saved in /data/data/com.fm.sg.android/files/fileName.p12
What am I doing wrong?
path = context.getFilesDir().getAbsolutePath().toString();
String filename = "fileName.p12";
File file = new File(path,filename);
then the rest of your code....this should make it...
I have a little issue with creating folders for my application in Internal Memory. I'm using this piece of code :
public static void createFoldersInInternalStorage(Context context){
try {
File usersFolder = context.getDir("users", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(usersFolder, "users.txt"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
File dataFolder = context.getDir("data", Context.MODE_PRIVATE);
File fileWithinMyDir2 = new File(dataFolder, "data.txt"); //Getting a file within the dir.
FileOutputStream out2 = new FileOutputStream(fileWithinMyDir2); //Use the stream as usual to write into the file.
File publicFolder = context.getDir("public", Context.MODE_PRIVATE);
File fileWithinMyDir3 = new File(publicFolder, "public.txt"); //Getting a file within the dir.
FileOutputStream out3 = new FileOutputStream(fileWithinMyDir3); //Use the stream as usual to write into the file.
} catch(FileNotFoundException e){
e.printStackTrace();
}
}
So folders are created but in front of their name there is the beginning "app_" : app_users, app_data, app_public. Is there a way to create the folders with the name given by me? And another question : I want to first create folder Documents and than all other folders "Data, Public, Users" on it.... And the last question : How can I give the right folder path if I wanted to create a file in Documents/Users/myfile.txt in Internal Memory?
Thanks in advance!
You can use this :
File myDir = context.getFilesDir();
String filename = "documents/users/userId/imagename.png";
File file = new File(myDir, filename);
file.createNewFile();
file.mkdirs();
FileOutputStream fos = new FileOutputStream(file);
fos.write(mediaCardBuffer);
fos.flush();
fos.close();
Is there a way to create the folders with the name given by me?
Use getFilesDir() and Java file I/O instead of getDir().
How can I give the right folder path if I wanted to create a file in Documents/Users/myfile.txt in Internal Memory?
Use getFilesDir() and Java file I/O, such as the File constructor that takes a File and a String to assemble a path.
I have downloaded a file from HttpConnection using the FileOutputStream in android and now its being written in phone's internal memory on path as i found it in File Explorer
/data/data/com.example.packagename/files/123.ics
Now, I want to open & read the file content from phone's internal memory to UI. I tried to do it by using the FileInputStream, I have given just filename with extension to open it but I am not sure how to mention the file path for file in internal memory,as it forces the application to close.
Any suggestions?
This is what I am doing:
try
{
FileInputStream fileIn;
fileIn = openFileInput("123.ics");
InputStream in = null;
EditText Userid = (EditText) findViewById(R.id.user_id);
byte[] buffer = new byte[1024];
int len = 0;
while ( (len = in.read(buffer)) > 0 )
{
Userid.setText(fileIn.read(buffer, 0, len));
}
fileIn.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
String filePath = context.getFilesDir().getAbsolutePath();//returns current directory.
File file = new File(filePath, fileName);
Similar post here
read file from phone memory
If the file is where you say it is, and your application is com.example.packagename, then calling openFileInput("123.ics"); will return you a FileInputStream on the file in question.
Or, call getFilesDir() to get a File object pointing to /data/data/com.example.packagename/files, and work from there.
I am using this code to open file in internal storage. i think i could help.
File str = new File("/data/data/com.xlabz.FlagTest/files/","hello_file.xml");