I develop application with imageview, my problem is when I clicked button load picture, then will be shown gallery, then I want to copy selected image to res/drawable android, is it possible? if yes, can give me source code ?
Regards,
You cannot write to res/drawable. You can download image which can be stored in sdcard but you can't add images dynamically to drawable folder.
i am afraid res/ is read-only while running. Instead they give us the option or create files and our personal data in our application space
=====
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/app_name");
dir.mkdirs();
File file = new File(dir, "myAppData.jpg");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println(bytes);// bytes would be data from the source file
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
//..
} catch (IOException e) {
//...
}
Related
In my application, I want to create a text file in the cache folder and first what I do is create a folder in the cache directory.
File myDir = new File(getCacheDir(), "MySecretFolder");
myDir.mkdir();
Then I want to create a text file in that created folder using the following code that doesn't seem to make it there. Instead, the code below creates the text file in the "files" folder that is in the same directory as the "cache" folder.
FileOutputStream fOut = null;
try {
fOut = openFileOutput("secret.txt",MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String str = "data";
try {
fOut.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
SO my question is, how do I properly designate the "MySecretFolder" to make the text file in?
I have tried the following:
"/data/data/com.example.myandroid.cuecards/cache/MySecretFolder", but it crashes my entire app if I try that. How should I properly save the text file in the cache/MySecretFolder?
use getCacheDir(). It returns the absolute path to the application-specific cache directory on the filesystem. Then you can create your directory
File myDir = new File(getCacheDir(), "folder");
myDir.mkdir();
Please try this maybe helps you.
Ok, If you want to create the TextFile in Specific Folder then You can try to below code.
try {
String rootPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/MyFolder/";
File root = new File(rootPath);
if (!root.exists()) {
root.mkdirs();
}
File f = new File(rootPath + "mttext.txt");
if (f.exists()) {
f.delete();
}
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Just change
fOut = openFileOutput("secret.txt",MODE_PRIVATE);
to
fOut = openFileOutput(myDir+"/secret.txt",MODE_PRIVATE);
This will make secret.txt under MySecretFolder
getPrivateDir will create a folder in your private area (Context.MODE_WORLD_WRITEABLE- use what suits you from Context.MODE_...)
public File getPrivateDir(String name)
{
return context.getDir(name, Context.MODE_WORLD_WRITEABLE);
}
openPrivateFileInput will create a file if it doesn't exist in your private folder in files directory and return a FileInputStream :
/data/data/your.packagename/files
Your application private folder is in
/data/data/your.packagename
public FileInputStream openPrivateFileInput(String name) throws FileNotFoundException
{
return context.openFileInput(name);
}
If you package name is uno.due.com your app private folder is:
/data/data/uno.due.com
All directories underneath are weather created by you or by android for you. When you create a file as above it will go under:
/data/data/uno.due.com/files
Simple and easy code to create folder, file and write/append into the file
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newfoldername/"; // it will return root directory of internal storage
File root = new File(path);
if (!root.exists()) {
root.mkdirs(); // create folder if not exist
}
File file = new File(rootPath + "log.txt");
if (!file.exists()) {
file.createNewFile(); // create file if not exist
}
BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
buf.append("hi this will write in to file");
buf.newLine(); // pointer will be nextline
buf.close();
}
catch (Exception e) {
e.printStackTrace();
}
NOTE: It needs the Android External Storage Permission so add below line in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
I'm writing an Android application in which I want to create text files in a particular folder and afterwards I want to read the files from my device.
I'm doing this way:
File sd = Environment.getExternalStorageDirectory();
File f;
FileWriter fw = null;
String path = sd.getAbsolutePath() + "/Samples/";
f = new File(path+File.separator+"filename.txt");
if (!f.exists())
{
f.mkdirs();//Creates the directory named by this file, creating missing parent directories if necessary
try
{
f.createNewFile();
//fw = new FileWriter(f, true);
}
catch (IOException e)
{
Log.e("ERROR","Exception while creating file:"+e.toString());
}
The problem is that in this way I create another folder instead of a text file. What can I do? Thanks
Instead of:
f.mkdirs();
do:
path.mkdirs();
I found the solution and I want to share it with you:
File sd = Environment.getExternalStorageDirectory();
File folder;
String path = sd.getAbsolutePath() ;
folder = new File(path, dirName);
if (!folder.exists()){
folder.mkdirs();}
try{
File file = new File(folder, fileName+".txt");
file.createNewFile();
} catch (IOException e) {
Log.e("ERROR", "Exception while creating file:" + e.toString());
}
I hope this could help other people having the same problem. Good luck
I use the code below in order to save a file in my sdcard. The code is taken by another post in stackoverflow. The file is written in the card successfully and i can view it in my phone. However i noticed that i cannot view the file when i connect my phone in the computer. I also noticed that the contents of the folder ext_card are showing in my computer and not the contents of the folder sdcard.
Here is the code
File dir = new File ("/mnt/ext_card" + "/mine");
dir.mkdirs();
File file = new File(dir, "capture.csv");
try {
f = new FileOutputStream(file);
captureFile = new PrintWriter(f);
} catch (IOException ex) {
}
I write it inside the ext folder and i cannot view it again.
Please help...
I have created a text file in the first Activity of my App, which is all working fine.
In My next Activity, i want to append to the text file.
But when i try to append the catch throws up the following error..
mnt/scard/PatRecords/testfile.txt contains a file seperator
And nothing is added to the file.
my code for appending is..
try {
File directory = new File
(Environment.getExternalStorageDirectory().getPath()+"/PatRecords");
FileOutputStream fOut = openFileOutput(directory.getPath()+"/"+FileName$, MODE_APPEND);
OutputStreamWriter OutWriter = new OutputStreamWriter(fOut);
OutWriter.write(TestNo$+"\n");
OutWriter.write(Date$+"\n");
OutWriter.close();
fOut.close();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
Error=1;
}//End of try/catch
I have tried removing the seperators etc but still doesn't work, and as far as i can see
the path shown in the catch error is correct...?
openFileOutput() is for opening files in your program data folder, which is located on the internal memory, you may only supply the file name, not the path, hence the complain mnt/scard/PatRecords/testfile.txt contains a file separator.
if you want to open files on the SD card, you have to use FileOutputStream() or something like that:
File of = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
i am saving video and image in a folder ..now i want to make this folder as password protected , means while opening this folder needs to enter a password for view the file inside it
hope here ill get any relevant answer for doing this...if there some any other possible please suggest..
try {
dirName = "/mydirectory/";
fileName = new Long(
SystemClock.currentThreadTimeMillis())
.toString()
+ ".png";
} catch (NullPointerException e) {
// TODO: handle exception
}
try {
if (android.os.Environment
.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED)) {
File sdCard = Environment
.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath()
+ dirName);
dir.mkdirs();
File file = new File(storedImagePath);
os = new FileOutputStream(file, true);
byte[] byteArray = receivedImageData.getBytes();
byteArray = Base64.decode(byteArray, 0);
os.write(byteArray);
os.flush();
os.close();
} else {
}
} catch (Exception e) {
}
I'd like to suggest a different/feasible approach, Encrypt your file!
Look at this answer!
Even if you are successful in implementing a password protection (Wow!) here are the cons,
This will only protect when your app is running.
SD cards are supposed to be transferred(Hence your app cannot protect the files on SDcard always).