Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓,如何在内存中存储图像?_Android_Bitmap - Fatal编程技术网

Android 安卓,如何在内存中存储图像?

Android 安卓,如何在内存中存储图像?,android,bitmap,Android,Bitmap,我想在内部存储器(不是外部存储器)上存储位图图像。我已经写了这段代码,但似乎有什么问题。因为当我从DDMS下载图像时,我无法打开它 public String writeFileToInternalStorage(Context context, Bitmap outputImage) { String fileName = Long.toString(System.currentTimeMillis()) + ".png"; try {

我想在内部存储器(不是外部存储器)上存储位图图像。我已经写了这段代码,但似乎有什么问题。因为当我从DDMS下载图像时,我无法打开它

public String writeFileToInternalStorage(Context context, Bitmap outputImage) {

        String fileName = Long.toString(System.currentTimeMillis()) + ".png";

        try {
            OutputStreamWriter osw = new OutputStreamWriter(context.openFileOutput(fileName, Context.MODE_PRIVATE));
            osw.write(outputImage.toString());
            Log.i(TAG, "Image stored at: " + fileName);
        } catch (Exception e) {
            Log.w(TAG, e.toString()); 
            fileName = null;
        } 

        return fileName;
    } 

问题是使用.toString()而不是将位图压缩为FileOutputStream:

FileOutputStream out = new FileOutputStream(filename);
outputImage.compress(Bitmap.CompressFormat.PNG, 90, out);
内部存储器也可以通过上下文检索

File cacheDir = context.getCacheDir();
toString()不是图像:)你放在文件上的contant不是二进制数据,而是一些字符串

一种方法是:

public String writeFileToInternalStorage(Context context, Bitmap outputImage) {
    String fileName = Long.toString(System.currentTimeMillis()) + ".png";

    final FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
    outputImage.compress(CompressFormat.PNG, 90, fos);
}

我直接在浏览器中编码,可能有一些语法错误,但代码应该可以工作。

您正在尝试使用
outputImage.toString()
位图
写入
字符串
。我甚至不知道这会给你带来什么,但我认为这不是一个有效的图像。谢谢亲爱的Zelter,但不幸的是,当我使用此代码时,新的红线出现在“openFileOutput”下面,我无法运行该应用程序。它说类中没有方法,并要求我创建它。调用
上下文。openFileOutput
谢谢亲爱的Zelter,它工作正常。只是,你能告诉我怎么得到图像地址吗?因为在保存图像后,我应该保存它到数据库的路径,以供以后参考。谢谢,我找到了出路字符串文件名=Long.toString(System.currentTimeMillis())+“.png”;File cacheDir=context.getFilesDir();字符串路径=cacheDir.getPath()+文件名;谢谢你,蒂姆,但是效果不太好。它抛出“FileNotFoundException”。来自logcat的完整消息是:java.io.FileNotFoundException:/1342996573233.png:打开失败:EROFS(只读文件系统)