gpt4 book ai didi

java - 无法将图像保存到 SD 卡

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:28 29 4
gpt4 key购买 nike

我尝试使用此代码下载图像并保存:在这一行中:

OutputStream os =new FileOutputStream(f);

我在模拟器上试了一下。我的应用程序得到异常,没有权限保存到这个路径:

java.io.FileNotFoundException: /mnt/sdcard/InterFlora/1221618532: open failed: EACCES (Permission denied)

我还添加了这一行:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这是代码:

   RetreiveImage extends AsyncTask<String, Void,Bitmap> {
Item m_item;
File f;

public RetreiveImage(Item aItem , File aFile){
m_item = aItem;
f = File (m_Sdcard.getAbsolutePath() + "/myFolder");
}

@Override
protected Bitmap doInBackground(String... params) {

try
{
Bitmap bitmap=null;
InputStream is=new URL(m_item.small).openStream();
OutputStream os =new FileOutputStream(f);
os.close();

return bitmap;
}
catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}
}
}

编辑

File m_Sdcard = Environment.getExternalStorageDirectory();
File cacheDir =new File (m_Sdcard.getAbsolutePath() + "/MyFolder");
if(!cacheDir.exists())
cacheDir.mkdirs();

最佳答案

将此行添加到 manifest.xml 有效!!!

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于java - 无法将图像保存到 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9334090/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com