【发布时间】:2011-05-18 10:37:32
【问题描述】:
如何将位图图像写入内部存储,(如果我可以将位图保存在我自己的目录中,例如“/data/data/com.myapp/myfolder/img1.png”)我需要保存将多个图像保存到此目录中。将多个图像保存到上述目录后,我需要在列表活动中显示保存的图像,
方法用于将图像保存到内部存储
private void saveToInternalSorage(Bitmap bitmapImage,String filename){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("myfolder", Context.MODE_PRIVATE);
File mypath=new File(directory,filename+ ".png");
FileOutputStream fos = null;
try {
// fos = openFileOutput(filename, Context.MODE_PRIVATE);
System.out.println("mypath = "+mypath);
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
从内部存储中读取图像。
private File [] loadInternalImages(){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("myfolder", Context.MODE_PRIVATE);
File[] imageList = directory.listFiles();
if(imageList == null){
imageList = new File[0];
}
Log.i("My","ImageList Size = "+imageList.length);
return imageList;
}
问候, 山姆
【问题讨论】:
-
到目前为止你做了什么?你有什么烦恼?
-
我已经用我正在使用的方法更新了问题,当我读取内部存储时,我的列表大小为零。
-
做一些进一步的调查 - 检查您的位图是否正确保存在内部存储中。检查您的目录是否已创建,在您的代码中您没有创建它。
-
我可以得到保存图像的绝对路径,并且可以读取图像的高度。 System.out.println("mypath.getAbsolutePath() = "+mypath.getAbsolutePath()); BitmapFactory.Options bfo = new BitmapFactory.Options();位图 bm = BitmapFactory.decodeFile(mypath.getAbsolutePath(), bfo); System.out.println("bm.getHeight = "+bm.getHeight());问题是 System.out.println("cw.fileList().length = "+cw.fileList().length);返回零,
标签: android