Android - 将图像保存到 SD,然后加载图像

发布于 2024-10-25 07:11:14 字数 964 浏览 1 评论 0原文

我快速设计了一段代码,从指定的 URL 加载,然后将其保存到 SD 卡,但它没有保存到 SD 卡。

URL myFileUrl = new URL( Image_HTML);
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
try {
    HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); 
    conn.setDoInput(true); 
    conn.connect(); 
    InputStream is = conn.getInputStream(); 
    bm = BitmapFactory.decodeStream(is); 
    FileOutputStream fos = new FileOutputStream(filepath+image_name); 
    bm.compress(CompressFormat.PNG, 100, fos); 
    fos.flush(); 
    fos.close(); 

    bm = BitmapFactory.decodeFile(filepath+image_name);
    image_loader_view.setImageBitmap(bm);

} catch (FileNotFoundException e) {

    e.printStackTrace();
    Log.i("Hub", "FileNotFoundException: "+ e.toString());

} catch (IOException e) {

    e.printStackTrace();
    Log.i("Hub", "IOException: "+ e.toString());
}

我试图使这段代码尽可能轻量,并且我还在android清单中激活了EXTERNAL.STORAGE.WRITE。

I've designed quickly a piece of code which loads from an specified URL and then saves it to the SD card, however it is not saving to the SD.

URL myFileUrl = new URL( Image_HTML);
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
try {
    HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); 
    conn.setDoInput(true); 
    conn.connect(); 
    InputStream is = conn.getInputStream(); 
    bm = BitmapFactory.decodeStream(is); 
    FileOutputStream fos = new FileOutputStream(filepath+image_name); 
    bm.compress(CompressFormat.PNG, 100, fos); 
    fos.flush(); 
    fos.close(); 

    bm = BitmapFactory.decodeFile(filepath+image_name);
    image_loader_view.setImageBitmap(bm);

} catch (FileNotFoundException e) {

    e.printStackTrace();
    Log.i("Hub", "FileNotFoundException: "+ e.toString());

} catch (IOException e) {

    e.printStackTrace();
    Log.i("Hub", "IOException: "+ e.toString());
}

I have tried to make this code as lightweight as possible, and I have also activated the EXTERNAL.STORAGE.WRITE in the android manifest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-11-01 07:11:14

夫妇的事情。

当您使用 FileOutputStream 时,您必须确保在尝试向其中写入文件之前已创建要写入的目录。如果没有,你必须创建它。这可以通过 File 类的 mkdirs() 方法来完成。

接下来,由于 Android 使用的文件系统类型,我不确定是否需要调用 getAbsolutePath。我以前从来没有用过它来保存到 SD 上。

我会尝试这些,看看其中之一是否可以为您解决这个问题。

Couple things.

When you use a FileOutputStream you have to make sure the directory you are trying to write to is created before you try to write a file to it. If not you have to create it. This can be done via mkdirs() method of the File class.

Next I'm not sure the call to getAbsolutePath is required, due to the type of file system Android uses. I've never had to use it to save to SD before.

I'd try these and see if one of them will solve it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文