Getting Null Pointer Exception While Saving The Image To Sd Card
I want to save an image into sd card.But only the folder naming the image file is created in the sd card,but the actual file is not being created in the sd card.I am getting null p
Solution 1:
Have you added this permission
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Solution 2:
You call
success = folder.mkdirs();
That maks a folder named /sdcard (which exists) and a folder name /sdcard/sample.JPEG The path where you want to write your image is a directory. You can't open an OutputStream on a directory.
Solution 3:
Just simple change needs in your code, try as following,
Filefolder=newFile(Environment.getExternalStorageDirectory().toString());
booleansuccess=false;
if (!folder.exists()) {
success = folder.mkdirs();
}
Filefile=newFile(Environment.getExternalStorageDirectory().toString() + "/sample.JPEG");
if (!file.exists()) {
success = file.createNewFile();
}
Solution 4:
This works good in my project:
privatevoid saveChicaDress(Bitmap dress){
String path = Environment.getExternalStorageDirectory().toString() +
getResources().getString(R.string.path_to_store);
OutputStream fOut = null;
File file = new File(path, "chica.png");
int i = 1;
while(file.exists()){
file = new File(path, "chica("+String.valueOf(i)+").png");
++i;
}
file.getParentFile().mkdirs();
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dress.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
fOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and in AndroidManifest:
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Post a Comment for "Getting Null Pointer Exception While Saving The Image To Sd Card"