【问题标题】:Android Read Jpeg from internal Storage memoryAndroid 从内部存储器中读取 Jpeg
【发布时间】:2013-06-07 10:40:49
【问题描述】:

我将位图图像保存到 android 内部存储中。现在我想检索这个 /myphoto.jpg 的图像并使用 shareIntent 共享它。我如何从内部存储器中读取 myphoto.jpg。理想情况下,我希望获得一个文件路径。

    ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, outputBuffer);
    byte[] byteImage1=outputBuffer.toByteArray();


    //save file to internal storage
    FileOutputStream outputStream;

    try {
      outputStream = context.openFileOutput("myphoto.jpg", Context.MODE_PRIVATE);
      outputStream.write(byteImage1);
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }

【问题讨论】:

  • 打印你保存图片的代码。

标签: android android-memory


【解决方案1】:

您可以使用以下代码从您的 sdcard 中获取所有图像:

FileFilter filterForImageFolders = new FileFilter() 
    {            
        public boolean accept(File folder) 
        { 
            try 
            { 
                //Checking only directories, since we are checking for files within 
                //a directory 
                if(folder.isDirectory()) 
                { 
                    File[] listOfFiles = folder.listFiles(); 

                    if (listOfFiles == null) return false; 

                    //For each file in the directory... 
                    for (File file : listOfFiles) 
                    {                            
                        //Check if the extension is one of the supported filetypes                           
                        //imageExtensions is a String[] containing image filetypes (e.g. "png")
                        for (String ext : imageExtensions) 
                        { 
                            if (file.getName().endsWith("." + ext)) return true; 
                        } 
                    }                        
                } 
                return false; 
            } 
            catch (SecurityException e) 
            { 
                Log.v("debug", "Access Denied"); 
                return false; 
            } 
        } 
    };

现在像这样使用这个方法:

File extStore = Environment.getExternalStorageDirectory();
File[] imageDirs = extStore.listFiles(filterForImageFolders);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多