【问题标题】:how to save multiple bitmap images to internal storage and read into a list activity如何将多个位图图像保存到内部存储并读入列表活动
【发布时间】: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


【解决方案1】:

也许您可以尝试使用 imageList.size 而不是 imageList.length 来检查您的列表是否正确填写。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 2014-06-01
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多