android Downloading database to phone WITHOUT SD card - android

I have read this tutorial about downloading database to my sd card and I have a question what will happen while the mobile phone does not have SD card?
Then I have to download data to internal memory.
So how to check if device has got sd card or not and then set appropriate location before downloading? or maybe it will be done automatically because it use:
outFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
Please help me if you know,
Thank you

Some of Android phones doesnt have an sdcard slot but the internal memory is treated and simulated as external sdcard. Check this before.
To check the sd-card state: getExternalStorageState()
If it has sdcard (or simulated sdcard)
outFile = new File(Environment.getExternalStorageDirectory() + "/" + fileName);
If its better if you create a folder in the sdcard:
String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath();
File dir = new File(sdpath + "/AppFolder");
dir.mkdir();
outFile = new File(dir.toString() + "/" + fileName);
IF the phone dosnt have a simulated sdcard:
outFile = new File(fileName);
The location is data/data/mypackage/

I think it can be useful Android Storage Options.
For database you can check sd-card state with getExternalStorageState(), or use Android SQL helper-class.

public static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

Related

get external sdcard specific folder path for all android devices

Is there any common way to find the specific folder in micro SD card path all crevices?
how to get this path by something like
/storage/extSdCard/MYFOLDER
This is my code its only working on android 4.4.
String securepath = secStoreSystem.getenv("SECONDARY_STORAGE");
String defaul_directory_tpath = secStore+"/MYFOLDER";
secStoreSystem.getenv("SECONDARY_STORAGE")
getting null value in android 4.4+ devices
try this
path = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.txt";
//getting your file/folder
File f = new File(path + File.separator + fileName);
As per your code you are saving external storage path to string variable path
but your are using path1 for getting your folder. Please check this.

How to save image to external storage not save in internal

i dont understand why people say that is save in external storage because when i use this code and i check in my SD CARD is not have file
Code This one
OutputStream imageFileOS;
int imageNum = 0;
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), "Punch");
if(!imagesFolder.exists()){
imagesFolder.mkdirs(); // <----
}
String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
File output = new File(imagesFolder, fileName);
while (output.exists()){
imageNum++;
fileName = "image_" + String.valueOf(imageNum) + ".jpg";
output = new File(imagesFolder, fileName);
}
Uri uriSavedImage = Uri.fromFile(output);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
when i run code and check ,It have file in "Internal Memory/Pictures/Punch/image_0.jpg"
but not see in SD CARD (SD CARD = external memory card 16 GB)
Please help me ..
I think you are confused between what getExternalStorageDirectory does.
It gets the primary storage directory, specified by the the device manufacturer. This is usually "sdcard". "External_sd" as specified in your comment is infact a secondary storage directory, which will not be returned in the given method.
This is still not the protected internal storage, and can be mounted and accessed by Pcs when connected.
From android docs:
Note: don't be confused by the word "external" here. This directory
can better be thought as media/shared storage. It is a filesystem that
can hold a relatively large amount of data and that is shared across
all applications (does not enforce permissions). Traditionally this is
an SD card, but it may also be implemented as built-in storage in a
device that is distinct from the protected internal storage and can be
mounted as a filesystem on a computer.

Saving to device with no SD card

My friend and I are attempting to create an app that saves files to a device. We used this code to write to an external SD card, and it works great on his Droid X and Samsung Galaxy Tab.
Get the path to the SD card:
private static final File ROOT = Environment.getExternalStorageDirectory();
Create the folder path and files:
FileWriter fw = new FileWriter(ROOT + "/test/" + "time_frames.txt");
we are using document factory to create the documents
so you can see that we create the path then try to save to that path that was just created
File file = new File(ROOT + "/test/" + "time_frames.txt");
When I run it on my Nexus S (which does NOT have a SD card) is having trouble with the exact same code.
private static final File ROOTtest = Environment.getExternalStorageDirectory();
this returns /data
private static final File ROOT = Environment.getRootDirectory();
this returns /mnt/sdcard
private static final File intData = Environment.getDataDirectory();
this returns /system
my question is which one of these will work for devices that have SD cards and no SD cards? I have tried a lot, but trying all this stuff has really confused me. Thanks in advance
Environment.getExternalStorageDirectory() returns the path to external storage, it should work on all devices. Whether they have an actual SD card doesn't matter, and your code shouldn't care either. You need to make sure that external storage is available before you try to use it though, because it could be unmounted at any time.

Saving data on external storage

I want to save some data in the user's external directory (ie. SD card), but there seems to be a weird problem. I'm using Environment.getExternalStorageDirectory() which returns "mnt/sdcard/" (which is fine). I want to create two folders on in this directory so I do:
File main = new File(getExternalStorageDirectory() + "/my_app/some_data");
if(!main.isDirectory())
main.mkdirs();
Now I thought this would make the directory "mnt/sdcard/my_app/some_data", but after using a file manager to look at the SD card, it turns out that this folder is created at "mnt/sdcard/my_app/mnt/sdcard/my_app/some_data", which is quite bizarre. Can anyone tell me how to fix this?
Try the following and see what you get...
String packageName = this.getPackageName();
File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Android" + File.separator + "data" + File.separator + packageName + File.separator + "files");
myFilesDir.mkdirs();
It's exacly what I use to create a working directory on an SD card. For me it creates...
/mnt/sdcard/Android/data/com.mycompany.myApp/files
...where 'com.mycompany.myApp' is the actual package name of my app.

save image to sdcard android Directory problem

Im trying to save data to sdCard first i tried to saave it privately within app directory on externalStorage using getExternalFilesDir but gives me nullPointerException so i tried the other way given below it worked but when i want to store files into a custom directory that i want to named myself it give me error:
FileOutputStream os;
dirName = "/mydirectory/";
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(this.getExternalFilesDir(null), this.dirName+fileName); //this function give null pointer exception so im using other one
File file = new File(dir, dirName+fileName);
os = new FileOutputStream(file);
}else{
os = context.openFileOutput(fileName, MODE_PRIVATE);
}
resizedBitmap.compress(CompressFormat.PNG, 100, os);
os.flush();
os.close();
}catch(Exception e){
}
ErrorLog:
java.io.FileNotFoundException: /mnt/sdcard/mvc/mvc/myfile2.png (No such file or directory)
Your directory "/mnt/sdcard/mvc/mvc" may not exist. What about changing your path to store the image in the Environment.getExternalStorageDirectory() path and then working from there?
Also, as Robert pointed out, make sure you have write permission to external storage in your manifest.
Edit - to create directories:
String root = Environment.getExternalStorageDirectory().toString();
new File(root + "/mvc/mvc").mkdirs();
Then you can save a file to root + "/mvc/mvc/foo.png".
Have you requested permission to write onto SD card? Add the following string to you app manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You should check if you have added the required permission android.permission-group.STORAGE to your app. Without that permission you won't be able to access anything on the SD-Card.
BTW: On the Android system I know the SD-card is mounted on /sdcard not /mnt/sdcard
I found this book to be very helpful: "Pro Android Media: Developing Graphics, Music, Video, and Rich Media Apps for Smartphones and Tablets". I noticed a part that allows saving images and stuff to the SD card.

Categories

Resources