Android - copy files between internal storage - android

I would like to copy the files (image files captured from the camera app) between internal storage, from /data/data/[my app]/photo to another folder within the internal storage.
As you can see in the screen shot below, I have successfully changed the permission of both directories into '777' via the below code:
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("chmod 777 /data/data/[my app]/photo", null, new File("/data/data/[my app]/photo"));
So, I use the similar code to change the permission for the files in the "photo" folder but nothing happens.
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("chmod 777 /data/data/[my app]/photo/2_20130406_143344");
It would be very grateful if someone give me some direction as it has bothered me for the entire weekend.
Thanks a lot in advance!

If you are copying from one internal location to another and created by your app, you do not need additional permission. Try following
ContextWrapper wrapper = new ContextWrapper(context);
File sourceDirectory = wrapper.getDir(SOURCE_FOLDER_NAME, Context.MODE_PRIVATE);
File destinationDirectory = wrapper.getDir(DESTINATION_FOLDER_NAME, Context.MODE_PRIVATE);
Now use a recursive function to copy from source to destination
private boolean copyFiles(sourceDirectory, desitnationDirectory);
For, how to copy files recursively this.
Camera Images are stored on SD card and, you can always directly access those anytime.

Related

Why saves donwload-manager file to storage/emulated/0 directory in external storage?

I've read much about saving files, android directory structure, permissions, paths etc. but cant fix my problem.
What i have already
I requested WRITE and READ storage permissions and can create a folder in external storage.
What i want to do
I want to save files with download manager to this folder, i created.
The folder is in external storage (named "internal storage" in my android file explorer app) in the same directory such as "WhatsApp" or "Downloads"("/storage/emulated/0/sMyFolder" or "Internal storage/sMyFolder")
I've already tried to do so, but if i try to set setDestinationInExternalPublicDir() for download-managers request, the file will stored to the "storage" folder you can see in the screenshot (Internal storage/storage/emulated/0/sMyFolder/MyFile) and i dont know why it isnt stored in main directory in my "sMyFolder", you can also see in the picture.
Here my code:
Folder f;
private static void makeFolder(){
f = new File(Environment.getExternalStorageDirectory(), "sMyFolder");
if (!f.exists()) f.mkdirs();
}
public void download(){
// other code
makeFolder();
request.setDestinationInExternalPublicDir(f.getPath(), "MyFile1.txt");
}
Where is the problem with my code? Could someone help me please...
Thank you in advanced!

How to create public directory in internal memory in android

I want to create a sub directory which is non-private under the Environment.DIRECTORY_PICTURES directory. I used the code shown below but of no avail. The directory is created but it remains private. I don't know where I'm wrong.
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyImages");
file.mkdirs();
File f = new File(file,"Image1");
First, you have not created a file, at least in the code that is shown above. You have created a Java File object, and you created a directory, but you did not create a file for Image1, and so your directory is empty. I know of no way to force your empty directory to be picked up by anything, though you should see it if you use adb shell or DDMS to examine your device.
When you do eventually write a file to this directory, be sure to call getFD().sync() on the FileOutputStream before you close() that stream. Then, use MediaScannerConnection and its static scanFile() method to have your newly-created file be indexed by the MediaStore. Until you do this, your newly-created file will not be visible via MTP or many third-party apps.

Where should I choose to save text file in android?

I hope to export my data as a text file and save it to disk in Android, so I need to choose which folder I will save the file to.
I hope that a normal user can find the folder easily and the app does not need special permission to create the folder.
I have read some document, it seems that there are 3 ways: Context.getFilesDir().getAbsolutePath(), Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null).
You know some android users don't install SD card, so it seems that Environment.getExternalStorageDirectory().getAbsolutePath() and Context.getExternalFilesDir(null) are be excluded.
Am I only to choose Context.getFilesDir().getAbsolutePath()? or is there a better way? Thanks!
BTW, From the document Android - Where to save text files to?
Save it in internal phone storage, here no users and applications can access these files(unless if phone is rooted). But these files will be deleted one's the user selectes clear data from Settings -> Apps -> .
It seems that normal users can't access the saved text files if I use Context.getFilesDir().getAbsolutePath(), is it right?
Use this if you want a path that the user can modify and can have access
getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
More documentation here.
EDIT:
This is how use in case error in some devices:
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
String fname = "TEXT.txt";
File file = new File(path, fname);
if (!path.exists()) {
//noinspection ResultOfMethodCallIgnored
path.mkdir();
}
// YOUR CODE FOR COPY OR CREATE THE FILE TXT in PATH WITH THE VARIABLE file ABOVE

creating folder in android emulator

I am developing an android application. I need to create a folder in the internal memory, but when I try to create the folder I get the error below. I am running in an emulator.
mkdir failed for /mnt/New Folder , read only file system
I have tried many paths, but still the error persists. The only folder that I am able to create is called "cache", but I cannot browse it by my file chooser activity.
Any idea where is the suitable place to create folders without any permissions?
You can achieve it by this from a Context object (like Activity).
File files_folder = getFilesDir();
File files_child = new File(files_folder, "files_child");
files_child.mkdirs();
File created_folder = getDir("custom", MODE_PRIVATE);
File f1_child = new File(created_folder, "custom_child");
f1_child.mkdirs();
The function
getFilesDir()
will get the folder data/data/yourpackagename/files in internal memory. And the function
getDir("custom", MODE_PRIVATE)
will create a folder name app_custom in your app internal folder.
Answered by Minhtdh
I guess what you call internal memory is atualy the external memory (which can be open by
file chooser activity, the real internal memory only can be open if you have rooted)
If that true, you should chek those belows:
- first, you will need the write storeage permission in Manisfest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- then you should use `
String path =
Environment.getExternalStorageDirectory().getAbsolutePath() +
"/yourfoldername"
`
than
mnt/yourfoldername
at last you should use mkdirs to create folder than mkdir

Outputting a txt file to a folder in internal memory in android

I am in need to create a text file where the input is taken from the user.
I am trying to save the txt file in a folder(folder should be created even if it is not there)
My code is as follows,
FileOutputStream fileos = null;
if (FreeMemory() != 0) {
FileOutputStream fos=null;
try {
File path=new File(getFilesDir(),"sri");
if(!path.exists()){
path.mkdir();
}
File mypath=new File(path,"myfile.txt");
if (!mypath.exists()) {
fos = new FileOutputStream( mypath);
String text="Write Hii";
fos.write(text.getBytes());
fos.close();
}
}catch(Exception e){
}
}
The "path" variable gives this path: "/data/data/com.example.gm/files/sri"
I navigated to this path in my device: Android-->Data-->com.example.gm-->files-->
but the folder "sri" is not created and even the file also. Am I navigating to th ecorrect path ? I am not able to find out the file in the device.
I searched for the folder by installing "Astro File Manager" app too. But, couldn't find it. I am not getting any Exception when writing to the folder. The code must be correct. But where is the folder and file? Please anyone help me in solving this.
I want to save the txt file in internal memory of my device.
Whats wrong with my code? Please suggest me the solution.
I have gone through many trails and finally approached stackoverflow.
Thanks for any help!!
The data in the internal storage is not accessible out side the application owning it, so any third party application like file browser or Astro File Manager will not be able to see the files. The application with root access will only be able to access the files in the internal memory. I believe you should programmatically verify if the file is created. getFilesDir() will give the internal memory location allocated to that app.
Have you give the permission in manifiest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Categories

Resources