I am calling a function from onPostExecute of an Asynctask class like this...
protected void onPostExecute(String file_url)
{
SaveImageToSDCard(photo,photo_id);
}
Definition of the function is...
void SaveImageToSDCard(String Image, String photo_id)
{
try{
byte[] imageBytes=Base64.decode(Image, Base64.DEFAULT);
InputStream is = new ByteArrayInputStream(imageBytes);
Bitmap image=BitmapFactory.decodeStream(is);
String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Clubs/"+club_id;
if (!new File(mBaseFolderPath).exists()) {
new File(mBaseFolderPath).mkdir();
}
String mFilePath = mBaseFolderPath + "/" + photo_id + ".jpg";
File file = new File(mFilePath);
FileOutputStream stream = new FileOutputStream(file);
if (!file.exists()){
file.createNewFile();
}
image.compress(CompressFormat.JPEG, 100, stream);
is.close();
image.recycle();
stream.flush();
stream.close();
}
catch(Exception e)
{
Log.v("SaveFile",""+e);
}
}
I am getting the exception in SaveImageToSDCard() like this
java.io.FileNotFoundException: /storage/sdcard/Clubs/27/3.jpg: open failed: ENOENT (No such file or directory)
I know there is no such folder and file, but I coded it to create folder and file like this in the above function
if (!new File(mBaseFolderPath).exists()) {
new File(mBaseFolderPath).mkdir();
}
if (!file.exists()){
file.createNewFile();
}
I have uses permission too..
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
But, still having this exception. What should I change? Where am I doing the mistake?