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.
Related
I am using Android MediaController. How can I save a recorded voice file to internal storage?
private String FILE;
FILE = Environment.getExternalStorageDirectory() + "/tempRecord.3gpp";
Well here it describes how to save to internal storage
In short use:
File file = new File(context.getFilesDir(), filename);
Where context.getFilesDir() returns a File representing an internal directory for your app.
To write to cache change getFilesDir() to getCacheDir().
I'm trying to write a file to my phone.
I used Environment.getDataDirectory() to know the internal storage's path and Environment.getExternamStorageDirectory() to know the external storage's path.
But when I use Environment.getExternalStorageDirectory() as path, the file is created in internal storage. And when I use Environment.GetDataStorage() as the path, the file is not created. (I am not sure, but I can't find it in the explorer app, at least.)
I think my phone's internal storage is perceived as external storage.(In my case, it has 32 GB amount of storage)
I want to know removable storage(e.g. micro SD card) path. What should I do?
From the official documentation for getExternalStorageDirectory()
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.
So, it can be different from built-in storage in a device.
For your case, you could use getExternalStoragePublicDirectory(java.lang.String)
This is where the user will typically place and manage their own
files
The path here should be one of DIRECTORY_MUSIC, DIRECTORY_PODCASTS,
DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS,
DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or
DIRECTORY_DCIM. May not be null.
Or if you want your data to be deleted whenever the user uninstalls your app, you could use getExternalFilesDir().
As these files are internal to the applications, and not typically visible to the user as media.
Also there are some differences between getFilesDir() and getExternalFilesDir()
External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. See the APIs on environment for information in the storage state.
There is no security enforced with these files. For example, any application holding WRITE_EXTERNAL_STORAGE can write to these files.
Try this...
static String storagestate = Environment.getExternalStorageState();
private static FileOutputStream outStream;
private static File imageFilepath;
public static String saveImage(Bitmap bitmap) {
File folder = null;
// Check for SD card
if (storagestate.equals(Environment.MEDIA_MOUNTED)) {
folder = new File(Environment.getExternalStorageDirectory(),
"*YourStorageNameInDevice");
if (!folder.exists()) {
folder.mkdir();
}
outStream = null;
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
.format(new Date());
// Getting filepath
imageFilepath = new File(folder.getPath() + File.separator
+ timestamp + ".PNG");
try {
outStream = new FileOutputStream(imageFilepath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return imageFilepath.getAbsolutePath();
}
}
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);
}
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.
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.