如何在图库中创建文件夹并保存文件

时间:2018-06-20 06:54:15

标签: android file path copy

现在,我从外部存储文件夹复制mp4文件,并将复制文件保存在带有文件夹的图库中。但是我的图库应用程序没有我编写的文件夹。当然,也要归档。但是在文件浏览器中,DCIM文件夹中正确存在。

因此,如何使用我编写的文件夹将文件保存在图库中。如果可以解决此问题,请告诉我。

private void saveToGallery(String recVideoPath) {
    progressdialog = new CNetProgressdialog(this);
    progressdialog.show();

    String folderName = "DuetStar";
    String fromPath = recVideoPath;
    String toPath = Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DCIM;

    File toPathDir = new File(toPath + "/" + folderName);
    final File fromPathFile = new File(fromPath);
    File toPathFile = new File(toPath + "/" + folderName, recVideoPath.substring(recVideoPath.lastIndexOf("/") + 1, recVideoPath.length()));

    Log.d(TAG, "saveToGallery: " + RecordActivity.currentCreateFileName);
    Log.d(TAG, "saveToGallery: " + toPathDir.toString());
    Log.d(TAG, "saveToGallery: " + fromPath.toString());
    Log.d(TAG, "saveToGallery: " + toPath.toString());

    if (!toPathDir.exists()) {
        toPathDir.mkdir();
    } else {

    }

    FileInputStream fis = null;
    FileOutputStream fos = null;

    try {

        if (toPathDir.exists()) {
            fis = new FileInputStream(fromPathFile);
            fos = new FileOutputStream(toPathFile);

            byte[] byteArray = new byte[1024];
            int readInt = 0;

            while ((readInt = fis.read(byteArray)) > 0) {
                fos.write(byteArray, 0, readInt);
            }

            Log.d(TAG, "saveToGallery: " + readInt);

            fis.close();
            fos.close();

            Log.d(TAG, "saveToGallery: " + "Seucceful");
        } else {
            Toast.makeText(this, "There is no directory", Toast.LENGTH_SHORT).show();
        }
    } catch (IOException e) {
        e.getMessage();
    }

    progressdialog.dismiss();
}

1 个答案:

答案 0 :(得分:1)

您可以将其保存在特定的文件夹中,以查看此代码段以获取想法 String extStorageDirectory; extStorageDirectory = Environment.getExternalStorageDirectory().toString() + "/Video Folder name/"; //making the folder new File(extStorageDirectory).mkdirs();