简体   繁体   中英

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.

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). This is due to the File class in Java representing files and directories. And you simply cannot create directories inside a file.

Also add WRITE_EXTERNAL_STORAGE permission in your manifest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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