简体   繁体   English

应用程序文件夹在SD卡中不可见

[英]App folder not visible in sd-card

In my app i want to store camera images taken by the user in a folder.So for that i have created a folder ,but when erer i open my sd-card i cannot find that folder.Whats wrong with my code. 在我的应用程序中,我想将用户拍摄的相机图像存储在一个文件夹中。为此,我创建了一个文件夹,但是当我打开SD卡时,我找不到该文件夹​​。我的代码有什么问题。

Create FIle 创建文件

 protected void createFile(Context context, String mainName, String subName) {
        file = new File(Environment.getExternalStorageDirectory() + "/" + mainName + "/" + subName);
        if (!file.exists()) {
            file.mkdirs();
            Toast.makeText(getActivity(), "File created" + file.toString(), Toast.LENGTH_LONG).show();
        }
    }

Class where i am user the above method 我是上述方法的用户的类

public void onClick(View view) {

        setUp();

        createFile(getActivity(),"pocketDocs","Camera");
        switch (view.getId()) {

            case R.id.bt_choose_file:

                displayPopup(getActivity(), "Choose File", chooseDocumentArray, btChooseDoc, false, new GetNamePosition() {
                    @Override
                    public void getName(String name) {
                        userSelection = name;

                        if (userSelection.equals("Camera")) {
                            intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(intent, 1);
                            btChooseDoc.setText("Choose File");
                        }

Use 采用

file = new File(Environment.getExternalStorageDirectory() + "/" + mainName 
                + "/" + subName + "/");

Instead of 代替

file = new File(Environment.getExternalStorageDirectory() + "/" + mainName 
                + "/" + subName);

Without the trailing separator (in your case /), Android (which is based on UNIX), interprets this as a file (not a directory). 如果没有尾随分隔符(在您的情况下为/),则Android(基于UNIX)会将其解释为文件(而不是目录)。 This is due to the File class in Java representing files and directories. 这是由于Java中的File类表示文件和目录。 And you simply cannot create directories inside a file. 而且您根本无法在文件内创建目录。

Also add WRITE_EXTERNAL_STORAGE permission in your manifest. 还要在清单中添加WRITE_EXTERNAL_STORAGE权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM