如何在Android的内部缓存目录中创建映像文件 [英] How to create Image File inside Internal Cache Directory in Android

查看:81
本文介绍了如何在Android的内部缓存目录中创建映像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android的内部存储中的cache/images/中包含image.png.

I want to have image.png inside cache/images/ in Internal Storage of Android.

我无法通过以下代码获取它:

I am not able to have it with following code:

File directory = new File(getContext().getCacheDir(), "images");
directory.mkdirs();


File mypath=new File(directory,"image.png");

FileOutputStream fos = null;
try {
    fos = new FileOutputStream(mypath);

    bmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}       

使用以上代码,我什至无法创建名为images的目录.请帮助,我是初学者.

With the above code I am not even able to create a directory named images. Pls help, I am beginner.

推荐答案

尝试一下:

File sd = getCacheDir();
            File folder = new File(sd, "/myfolder/");
            if (!folder.exists()) {
                if (!folder.mkdir()) {
                    Log.e("ERROR", "Cannot create a directory!");
                } else {
                    folder.mkdirs();
                }
            }

            File fileName = new File(folder,"mypic.jpg");

            try {
                        FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName));
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                        outputStream.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

这篇关于如何在Android的内部缓存目录中创建映像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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