Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 27-Jun-14 22:02pm
v2

1 solution

Found my mistake, since I am creating more than one directory I must have used mkdirs() instead of mkdir().

The below snippet cleared my problem
if (!new File(mBaseFolderPath).exists()) {
 	new File(mBaseFolderPath).mkdirs();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900