将编码后的图像字符串保存为SD卡上的图像(jpg) [英] Saving encoded image string as image(jpg) on sdcard

查看:71
本文介绍了将编码后的图像字符串保存为SD卡上的图像(jpg)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这样的Asynctask类的onPostExecute调用一个函数...

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);
	}
}



我在这样的SaveImageToSDCard()中得到例外


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?

推荐答案

发现我的错误,因为我创建了多个目录,我必须使用mkdirs()而不是mkdir()。 br />


以下片段解决了我的问题

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();
}


这篇关于将编码后的图像字符串保存为SD卡上的图像(jpg)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆