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" />
Related
I would like to delete an internal file at runtime. When I download from an external server, the old version of the file (with the same name) is replaced, however I am unable to read it. I think that I need to delete the previous file before downloading the new version. Here is an example of what I have tried so far:
try {
FileOutputStream fos = getApplicationContext().openFileOutput("mytext.txt", Context.MODE_PRIVATE);
fos.write(getStringFromFile(pictosFile.getAbsolutePath()).getBytes());
Log.e("mytextfile",""+getStringFromFile(pictosFile.getAbsolutePath()));
progressDialog.cancel();
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
This allows me to save the file into internal memory, but I am unsure about how to delete the previous file before writing the new version.
If you need to ensure that the file is overwritten, i.e. delete an old copy before saving a new version, you can use the exists() method for a file object. Here is an example showing how to delete an old version of an image file before writing a new file with the same name in a nested directory:
// Here TARGET_BASE_PATH is the path to the base folder
// where the file is to be stored
// 1 - Check that the file exists and delete if it does
File myDir = new File(TARGET_BASE_PATH);
// Create the nested directory structure if it does not exist (first write)
if(!myDir.exists())
myDir.mkdirs();
String fname = "my_new_image.jpg";
File file = new File(myDir,fname);
// Delete the previous versions of the file if it exists
if(file.exists())
file.delete();
String filename = file.toString();
BufferedOutputStream bos = null;
// 2 - Write the new version of the file to the same location
try{
bos = new BufferedOutputStream(new FileOutputStream(filename));
Bitmap bmp = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(buf);
bmp.compress(Bitmap.CompressFormat.PNG,90,bos);
bmp.recycle();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
finally{
try{
if(bos != null)
bos.close();
}
catch(IOException e){
e.printStackTrace();
}
}
You must also ensure that you have read / write access to memory, make sure that you ask the user for these permissions at run time and have the following in your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
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'm working with camera in android and I'm new to android camera. I've followed the tutorial from here
Whenever I'll add External storage permission in android manifest, camera saves it in default directory without asking me and I want to save it in my own folder created in sd card. I'd searched a lot but couldn't find any useful link. Please help, any help will be much appreciated. Thank you :)
You can add this code in onActivityResult. This will store your image in a folder named "YourFolderName"
String extr = Environment.getExternalStorageDirectory().toString()
+ File.separator + "YourFolderName";
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
bitMap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(context.getContentResolver(),
bitMap, myPath.getPath(), fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
also need to set permission on Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
camera saves it in default directory without asking me and I want to save it in my own folder created in sd card
You can tell the camera where you would like the photo to be stored via EXTRA_OUTPUT, as is shown in the documentation's training module on ACTION_IMAGE_CAPTURE.
There is no requirement for all cameras to store images where you ask it to, though. If the file that you ask for does not exist after the image is taken, you will need to fall back to your existing application logic.
You can assign a path to write the image data to specific location like this:
String fileName = "/mnt/sdcard/foldername";
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(data);
I'm trying to figure how to store my application's data for the long term. Basically I get a list of data from from a web service, and I don't want to go back to the web service the next time the app runs. I'd prefer to just store it locally. How do I do this?
I don't mind serialising the data to any particular format. I don't see this on the Xamarin site for Android. There's a tutorial for iOS, but I'm not interested in that.
I personally copy the data from webservice in raw format in a text file on the memory.
So, I have just to open the inputStream from the file the same I did from the webservice and my code remains clean.
But I guess there are indeed thousands of ways to copy this data.
I just wanted to share the one I found more convenient.
The code just for information:
InputStream source = getStreamFromWebservice();// <= YOUR CODE HERE
File dir = context.getDir("CACHE", Context.MODE_PRIVATE);
dir.mkdirs();
File file = new File(dir, fileName);
// Write to Memory
try {
FileOutputStream f = new FileOutputStream(file);
byte[] buffer = new byte[32768];
int read;
try {
while ((read = source.read(buffer, 0, buffer.length)) > 0) {
f.write(buffer, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
}
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
}
I have a bitmap that I want to save to the sd card. I have 2 problems with that:
Set a name to the saved file
Save the file
I looked around Stack Overflow and couldn't find anything that worked, for both problems (mostly for the saving part, the set name part just got me all confused)
Is there a simple straight forward solution to this?
Thanks!
Bitmap yourBitmap; // you have to get your bitmap into this variable
String filePath = "/mnt/sdcard/"; // some times it may be only /sdcard not /mnt/sdcard
filePath += "newFileName.jpg";
try {
yourBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File(filePath)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You have to use below permission in manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Try this:
FileOutputStream out = new FileOutputStream(new File("/mnt/sdcard/pic.jpg"));
yourBitmap.compress(Bitmap.CompressFormat.JPG, 80, out);
out.close();