如何在android的内部存储器中打开保存在用户创建的文件夹中的图像

时间:2015-09-18 10:46:53

标签: android

我正在创建一个Android应用程序,我在其中编辑图像并将这些图像保存在我创建的文件夹中。 现在想要在按钮点击的网格视图中仅在我的应用程序中显示我编辑的所有图像。我能够通过此链接https://dzone.com/articles/displaying-images-sd-card显示保存在图库中的所有图像但我只想显示来自我的文件夹的图像

保存图像的代码是.......

RelativeLayout content = (RelativeLayout) findViewById(R.id.relative);
            content.setDrawingCacheEnabled(true);
            Bitmap bitmap = content.getDrawingCache();

            File myDir=new File("/sdcard/MyCollection");
            myDir.mkdirs();
            Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);
            String fname = "Image-"+ n +".jpg";
            File file = new File (myDir, fname);
            if (file.exists ()) file.delete ();
            boolean success = false;
            FileOutputStream outStream;
            try {

                outStream = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);

                outStream.flush();
                outStream.close();
                success = true;

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

1 个答案:

答案 0 :(得分:0)

请试试这个

Bitmap bitmap = null;
    File f = new File(cacheDir, String.valueOf("fileName"));
     try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        //o.inPreferredConfig =  Bitmap.Config.RGB_565; // Bitmap.Config.ARGB_8888;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
        int REQUIRED_SIZE1=120;
        if(isLargeImage){
            REQUIRED_SIZE1=250;
        }
        final int REQUIRED_SIZE = REQUIRED_SIZE1;
        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}