I want to store my Bitmap image into project directory. How can I have access to my project folder or what is the address of my project folder?
You have to put the images in the res/drawable folder. Then, you can access them by using: R.drawable.name_of_image (for name_of_image.png or name_of_image.jpg).
If you want to access them by their original name, you better save them in the assets folder. Then, you can access them by using the AssetManager:
AssetManager am = getResources().getAssets();
try {
InputStream is = am.open("image.png");
// use the input stream as you want
} catch (IOException e) {
e.printStackTrace();
}
If you want to save a programatically created image, you can do:
try {
FileOutputStream out = new FileOutputStream(context.getFilesDir().getAbsolutePath()+"/imagename.png");
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
}
You can't save it to your project directory. I recommend you to read the documentation about how android packages work, because it seems you don't understand.
Related
I want to store my database inside the app folder as like we save images, so that when uninstall the app it wont clear, is that possible to do.
Can anyone suggest how to achieve otherwise give me alternate options
Thanks in advance
The apps internal folder will always be deleted when you uninstall an application. What you want is to create an external folder, on the SDcard this will not go when you delete your application.
Example:
public void writeToSDCard() throws IOException {
System.out.println("Write To Sd");
File f=new File("/data/data/com.yourapp/databases/Bdr");
FileInputStream fis=null;
FileOutputStream fos=null;
try{
fis=new FileInputStream(f);
fos=new FileOutputStream("/mnt/sdcard/dumped.db");
while(true){
int i=fis.read();
if(i!=-1){
fos.write(i);
}
else{
break;
}
}
fos.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
fos.close();
fis.close();
}
catch(IOException ioe){
// System.out.println(ioe);
}
}
}
The above code will write your Database to the external SDCard with the name being equal to dumped.db and you will notice upon uninstall this will still persist on your SDcard
Dont forget to edit File f=new File("/data/data/com.yourapp/databases/Bdr"); according to your use case.
Also take special care that you dont hardcode the path to SDCard use Enviornmant.getExternalStorage() instead
I created A Buffered Reader, File Reader in my Android app and placed two text files inside raw folder. Now I have to pass a file path into File Reader, but what is the path of my file now?
BufferedReader reader = new BufferedReader(new FileReader("path"));
image link
but what is the path of my file now?
You don't have to pass file path into FileReader for reading file, here you can check following code snippet.
InputStream inputStream = null;
try {
inputStream = getResources().openRawResource(R.raw.hello_world);
byte[] reader = new byte[inputStream.available()];
while (inputStream.read(reader) != -1) {}
editField.setText(new String(reader));
editField.setSelection(editField.getText().length());
} catch(IOException e) {
Log.e(LOG_APP_TAG, e.getMessage());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
Log.e(LOG_APP_TAG, e.getMessage());
}
}
}
}
Raw folder is for what in ANDROID
Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.In above example R.raw.hello_world.
However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.
Raw mostly used with media files.
By default, you will be working in your present class path. So, you have to go back to the folder where the raw files is present and go inside it and access the files you want to.
file:///
will go back to your project directory, the one that contains all the folders. From there , you can access the raw folder.
I have 10 images and want to save all this images in android application memory . so whenever any use install this apps , he have already this all images .
i have hard coded many times and not get good response .
is there any way . so i will done it.
please help me
Store all your images in the assets folder of your APK. Once it gets installed, scan the internal memory to see if desired images are there. If not, copy them there. In this way, even when users clear data of your application, you can copy them back. Another good thing is the user will not know anything about it as well, so it a good user-experience. Only thing would be your APK size would increase, so manage accordingly.
Try the below piece of code
InputStream inputStream = getAssets().open("yourfile.jpg");
OutputStream out = new FileOutputStream(new File("/sdcard/yourfile.jpg"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
inputStream.close();
out.flush();
out.close();
You can keep the files in your asset or res folder , here I have kept the file in res/drawable/ and copy them on sdcard when I require. In below code first we check if file doesn't exist the we create a bitmap from the drawable and write the file out to sdcard.
File file = new File(pathExt+"/Pictures/", "s1.png");
if(isSDCARDMounted()){
if (!file.exists()) {
bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.s1);
try {
FileOutputStream outStream = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
isSDCARDMounted : function for checking if card is mounted or not
pathExt : variable for external storage directory path
Make sure have permission set for writing on external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am trying to write to an existing XML file in the /res/xml directory on an Android device. Two questions:
1) Are these files able to be overwritten?
2) If so, how can I obtain a handle to that file?
I am trying this to no avail:
FileOutputStream fos;
try {
fos = openFileOutput("/res/xml/scores.xml", Context.MODE_PRIVATE);
fos.write(writer.toString().getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UPDATE
Trying to read from the file using:
....
PriorityQueue<Score> scores = new PriorityQueue<Score>();
XmlPullParserFactory xmlFac = XmlPullParserFactory.newInstance();
XmlPullParser qXML = xmlFac.newPullParser();
File scoresFile = new File(c.getExternalFilesDir(null), "scores.xml");
InputStream is = new FileInputStream(scoresFile);
qXML.setInput(is,null);
....
Trying to write to the file using:
....
File scoresFile = new File(getExternalFilesDir(null), "scores.xml");
OutputStream os = new FileOutputStream(scoresFile);
os.write(writer.toString().getBytes());
os.flush();
os.close();
....
You need to use the external storage on the device. You can't and shouldn't be writing to the resources. Instead, copy the resource to the external storage, and then modify it there.
EDIT: You can also use your application's internal data folder to save files to, but again, you will want to copy your resource there if you want to modify it. This will allow the files to remain internal to your application.
I have several files stored in my project /res/values folder, is there any way to open and read these files from my android application? Each file contains text informations about one level of my game.
I really appreciate any help.
I find what I needed here:
http://developer.android.com/guide/topics/data/data-storage.html
"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). "
Sorry if My question was not clear.
And big thanks to Radek Suski for some additional information and example. I appreciate that.
As far I know you can either access files within the directory "files" from your project directory or from the SD-Card.
But no other files
EDIT
FileInputStream in = null;
InputStreamReader reader = null;
try {
char[] inputBuffer = new char[256];
in = openFileInput("myfile.txt");
reader = new InputStreamReader(in);
reader.read(inputBuffer);
String myText = new String(inputBuffer);
} catch (Exception e) {;}
finally {
try {
if (reader != null)reader.close();
} catch (IOException e) {; }
try {
if (in != null)in.close();
} catch (IOException e) {;}
}
Then your file will be located in:
/data/data/yourpackage/files/myfile.txt