I'm trying to develop a simple android app. I want to know if an image exist in one of my apps' folder, but I don't know what's the root of my app. I tried...
File nomeFile = new File("res/drawable-hdpi/imagename.jpg")
File nomeFile = new File("myappanem/res/drawable-hdpi/imagename.jpg")
...but it doesn't work.
Try This :
File YourFile = new File(Environment.getExternalStorageDirectory(), "/Android/data/....yourfile.txt");
No you cant access drawable or manifest after packaging. because there will be no "drawable" folder or manifest file at all. All your drawables packed in APK file after installation. You cant reach them.
But you can access any folder of your phone in your app like that.
Firstly make directory.
File directory = new File(Environment.getExternalStorageDirectory()+File.separator
+"Your folder");
directory.mkdirs();
Access your folder like that.
String fileUrl = "/Your folder/a.png";
String file = android.os.Environment.getExternalStorageDirectory().getPath() +
fileUrl;
File f = new File(file);
whether your taking about internal memory of your application cache directory
//use this for internal memory
String path =Environment.getDataDirectory().getAbsolutePath();
File myImage=new File(path,"your file name.extension");
//use this for cache directory
String path=getApplicationContext().getDir("" , Context.MODE_PRIVATE);
File myImage=new File(path,"your file name.extension")
Related
Could you please help me. renameTo() leaves empty old file. So I see 2 files in file system with new name and old name. The size of old file is 0. If I delete old file after renaming it says that file does not exist while staying in file system.
An absolute path of directory is:
/storage/sdcard0/DCIM/Camera
My code:
String dir = oldpath.substring(0, oldpath.lastIndexOf("/"));
File directory = new File(dir);
File from = new File(directory, oldfilename);
File to = new File(directory, newname);
renamed = from.renameTo(to);
Try this code:
File sdcard = new File("/storage/sdcard0/DCIM/Camera");
File from = new File(sdcard, "from.txt"); // Don't forget to set the file extension.
File to = new File(sdcard, "to.txt"); // In this case, we have a '.txt' file extension.
from.renameTo(to);
You can get the sdcard directory in String type programmatically by using this code:
String sdcard = Environment.getExternalStorageDirectory().getPath();
Don't forget to add this permission in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
RenameTo leaves an empty copy of the original file if the file is opened by another process.
For example, I wanted to rename a file after the DownloadManager finished downloading it. The DownloadManager apparently notifies the BroadcastReceivers after the download, but before closing the file. This caused the renameTo in the onReceive to leave an empty copy. To solve this problem, I had to make the BroadcastReceiver to wait half a second before renaming the file.
You must remember about two things : The file and the file extension (file type). The following is wrong way sometimes, in case of deleting and renaming file :
File file = new File (dir+"/"+myfile)
The right way is :
File file = new File (dir, myfile+".db");
For the Full Aprroach, you could look at Answer Here.
in my app I am seeing few crashes when I try to create a directory structure under the application internal storage, such as /data/data/[pkgname]/x/y/z....
Here is the failing code:
File clusterDirectory = new File(MyApplication.getContext().getFilesDir(), "store");
File baseDirectory = new File(clusterDirectory, "data");
if (!baseDirectory.exists()) {
if (!baseDirectory.mkdirs()) {
throw new RuntimeException("Can't create the directory: " + baseDirectory);
}
}
My code is throwing the exception when trying to create the following path:
java.lang.RuntimeException: Can't create the directory: /data/data/my.app.pkgname/files/store/data
My manifest specifies the permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />, even if it should not be necessary for this purpose (it is actually necessary for my apps due to Google Maps Android API v2).
It doesn't seem to be related to the phone, since I get this exception on old phones as well as on new ones (last crash report is Nexus 4 with Android 4.3).
My guess is that the directory /data/data/my.app.pkgname doesn't exist in the first place but mkdirs() can't create it because of permissions issues, could that be possible?
Any hints?
Thanks
Use getDir(String name, int mode) to create directory into internal memory. The method Retrieve, creating if needed, a new directory in which the application can place its own custom data files. You can use the returned File object to create and access files in this directory.
So example is
// Create directory into internal memory;
File mydir = context.getDir("mydir", Context.MODE_PRIVATE);
// Get a file myfile within the dir mydir.
File fileWithinMyDir = new File(mydir, "myfile");
// Use the stream as usual to write into the file.
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
For nested directories, you should use normal java method. Like
new File(parentDir, "childDir").mkdir();
So updated example should be
// Create directory into internal memory;
File mydir = getDir("mydir", Context.MODE_PRIVATE);
// Create sub-directory mysubdir
File mySubDir = new File(mydir, "mysubdir");
mySubDir.mkdir();
// Get a file myfile within the dir mySubDir.
File fileWithinMyDir = new File(mySubDir, "myfile");
// Use the stream as usual to write into the file.
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
I want to use the sdcard as a storage support of my application's data, the problem I encountered the path varies depending on the manufacturer, I do not have the same configuration for all tablets. I used the code below.
File root = Environment.getExternalStorageDirectory();
String Path = root.getAbsolutePath().toString();
File mFile = new File(Path+ "/fileName");
if(mFile.exists()){
mFile.delete();
}
With some tablets the job is done and the file is deleted with other no. So can you tell me how to get the external storage for all tablets.
File has a constructor that takes two parameters. A file and a String.
File root = Environment.getExternalStorageDirectory();
File mFile = new File(root, "fileName");
if(mFile.exists()){
mFile.delete();
}
Android provides many options for storing data persistently on the device. I have opted for internal storage, so please don't make suggestions for storing on an SD card. (I've seen too many questions about internal storage have answers for SD cards!!)
I would like to create subdirectories in my application's internal storage directory. I followed this SO answer, reproduced below.
File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
Unfortunately, that's creating subdirectories with "app_" prefixes. So, from the example, the subdirectory looks like "app_mydir" (not ideal).
The answer to this SO question suggests that you can get rid of the "app_" prefix this way:
m_applicationDir = new File(this.getFilesDir() + "");
m_picturesDir = new File(m_applicationDir + "/mydir");
But I want to write a zip to something like /data/data/com.mypackages/files/mydir/the.zip.
So, in the end, my code looks like this:
File appDir = new File(getApplicationContext().getFilesDir() + "");
File subDir = new File(appDir + "/subDir");
File outFile = new File(subDir, "/creative.zip");
But, this is creating another error: "File does not exist" when I try this:
FileOutputStream fileStream = new FileOutputStream(outFile);
How can I (1) create a subdirectory without the "app_" prefix and (2) write a zip into it?
Also, if my first demand isn't reasonable, tell me why in the comments! I'm sure "app_" prefix has some meaning that escapes me atm.
Did you create the directories?
File appDir = getApplicationContext().getFilesDir();
File subDir = new File(appDir, "subDir");
if( !subDir.exists() )
subDir.mkdir();
File outFile = new File(subDir, "creative.zip");
Note that you shouldn't use / anywhere in your app, just in case it changes. If you do want to create your own paths, use File.separator.
How do i make directories in internal storage?
I tried this:
File file = getFilesDir();
this makes me goes to folder "/data/data/com.mypackages/files/"
Then i want to make a folder again in that directories, let's say i want to make "myfiles" folder in there so it becomes, "/data/data/com.mypackages/files/myfiles/".
Can anyone tell me how?
I also tried this:
File file = getDir("myfiles", MODE_PRIVATE);
It makes the folder, but it was created with "app_", so the directories becomes "/data/data/com.mypackages/app_myfiles". I don't want that because i can't read the folder if it has "app_" in there.
The solution is under your eyes :D
m_applicationDir = new File(this.getFilesDir() + "");
m_picturesDir = new File(m_applicationDir + "/pictures");
With this code, i save in m_applicationDir the dir of the package (in your case the dir saved in file).
Then simply create a sub-directory named pictures.
So m_picturesDir points to:
/data/data/com.mypackages/files/pictures