如何在android内部内存的默认图像路径中创建文件夹?

如何在android内部内存的默认图像路径中创建文件夹?,android,Android,在我们的应用程序中,我们将图像存储到内存中。在获取数据时,我们通过内存的图像路径获取图像。借助绝对路径,我可以获取路径,如“./image/file.png”。但我需要文件夹的路径,如“./images/filefolder/file.png”。我的要求是删除所有图像,如果我试图打开另一个图像。所以我尝试将图像存储到一个文件夹中。通过它的引用,我可以删除文件夹中的内容。 请需要帮助 提前感谢, AA 上述代码用于在SD卡中创建文件夹。使用以下代码将图像保存到内部目录 private String

在我们的应用程序中,我们将图像存储到内存中。在获取数据时,我们通过内存的图像路径获取图像。借助绝对路径,我可以获取路径,如“./image/file.png”。但我需要文件夹的路径,如“./images/filefolder/file.png”。我的要求是删除所有图像,如果我试图打开另一个图像。所以我尝试将图像存储到一个文件夹中。通过它的引用,我可以删除文件夹中的内容。 请需要帮助

提前感谢, AA


上述代码用于在SD卡中创建文件夹。

使用以下代码将图像保存到内部目录

private String saveToInternalSorage(Bitmap bitmapImage){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
     // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,"profile.jpg");

    FileOutputStream fos = null;
    try {           

        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);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return directory.getAbsolutePath();
}
说明:

1.将使用给定名称创建目录。Javadocs用于告诉它将在何处创建目录

private String saveToInternalSorage(Bitmap bitmapImage){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
     // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,"profile.jpg");

    FileOutputStream fos = null;
    try {           

        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);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return directory.getAbsolutePath();
}
2.您必须提供要保存它的图像名称

从内存中读取文件。使用下面的代码

private void loadImageFromStorage(String path)
{

try {
    File f=new File(path, "profile.jpg");
    Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
        ImageView img=(ImageView)findViewById(R.id.imgPicker);
    img.setImageBitmap(b);
} 
catch (FileNotFoundException e) 
{
    e.printStackTrace();
}}
上述代码检查,如果您的内存空间大于,则可以在内存中创建文件夹。否则,将自动在SD卡中创建文件夹。。 此函数调用:

File file = null;
        long freeblock = getSdCardFreeBlockSize();
        // 0> internal memeory
        // 0=no memory

您是在内部存储器(数据文件夹内)还是在sd卡内添加图像?我正在尝试将图像存储到内部存储器而不是sd卡中。请尝试获取文件夹路径
String path=YOUR_FILE.getAbsolutePath();字符串folderpath=path.substring(0,path.lastIndexOf('/')-1)那么问题出在哪里,只需删除目录?您还可以通过编程方式删除文件夹。delete();我希望它对您有用。我正在尝试将图像存储到内部内存中,而不是存储在sd卡中。如果在您的设备中,sd卡不可用,则上面的代码是在您的内部内存中创建文件夹memory@dipali
getExternalStorageDirectory()
此行在外部存储器上创建文件。
File file = null;
        long freeblock = getSdCardFreeBlockSize();
        // 0> internal memeory
        // 0=no memory