Created folder is not visible in the file explorer..
Asked Answered
C

7

7

I have a problem with creating a folder and a file on the sdcard.

Here's the code:

    File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/folder");
    boolean success;
    if (!folder.exists()) {
        success = folder.mkdirs();
    }
    File obdt = new File(folder, "file.txt");
    try {
        success = obdt.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

With this code I expect to create the folderfolder in the Download folder of the sdcard and in this the file file. I want that the user can access the file. So I want to put it in a shared folder.
The success variable is true and when I run the code again the folder already exists and doesnt come in the if-block.
But I can't see the created folder and file on the sdcard in file explorer.

Info:getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() returns storage/sdcard/Download

I work with a Galaxy Nexus.

Culinarian answered 15/11, 2012 at 9:46 Comment(4)
do u have got any exceptions saying PERMISSION stuff? what if you initialize the success with the value of false?Goldshlag
no I have no exceptions...success is false when it is not initializedPilchard
Are you able to locate these folders "storage/sdcard/Download " ?Mutinous
no the storage folder is empty...I work under linux...maybe is there the problem?!Pilchard
C
3

Damn! :)

Now I solved my problem...I was misunderstanding the operation of creating files in the file system.
When I spoke of file explorer I meant the file explorer of the operating system and NOT the file explorer in the DDMS :).
I thought when I create a file I will see it in the file explorer of the operating system but when the device is connected to the PC the files can only be seen in the DDMS file explorer.
Sorry I'm new to Android ;)

When the App is running standalone without PC connection and afterwards I connect with the PC I see the created files and folders of course :)

Thanks for help

Culinarian answered 15/11, 2012 at 10:46 Comment(0)
L
0

If you haven't done so already, you will need to give your app the correct permission to write to the SD Card by adding the line below to your Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

If you have already done that see if :

File obdt = new File(/sdcard/folder/file.txt)
try {
    success = obdt.createNewFile();
} catch (IOException e1) {
    e1.printStackTrace();
}

works.

Lavellelaven answered 15/11, 2012 at 9:49 Comment(2)
the creating of File obdt = new File("/sdcard/file.txt"); is successfull but I don't see it in file explorer -.-Pilchard
That's werid. Will you try adb shell ls /sdcard/file.txt from terminal?Lavellelaven
S
0

Any errors from logcat?
Else: try something like Log.I("PATHNAME",folder.absolutePath()); and then look in your logcat to make sure where you are creating the folder where you think it is.

Stephaniestephannie answered 15/11, 2012 at 9:51 Comment(1)
path is: /storage/sdcard0/Download/folderPilchard
D
0

Try this out.

        File dir = new File(Environment.getExternalStorageDirectory()
                + "/XXX/Wallpapers/");
        File[] files = dir.listFiles();

        if (files == null)

        {
            int numberOfImages = 0;
            BitmapDrawable drawable = (BitmapDrawable) imageView
                    .getDrawable();
            Bitmap bitmap = drawable.getBitmap();
            File sdCardDirectory = Environment
                    .getExternalStorageDirectory();
            new File(sdCardDirectory + "/XXX/Wallpapers/").mkdirs();
            File image = new File(sdCardDirectory
                    + "/XXX/Wallpapers/Sample" + numberOfImages + ".JPG");

            boolean success = false;
            FileOutputStream outStream;
            try {
                outStream = new FileOutputStream(image);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                outStream.flush();
                outStream.close();
                success = true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();

            }
            if (success) {
                Toast.makeText(
                        getApplicationContext(),
                        "Image saved successfully in Sdcard/XXX/Wallpapers",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Error during image saving", Toast.LENGTH_LONG)
                        .show();
            }

Dont forget to add permission in manifest

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Dated answered 15/11, 2012 at 9:55 Comment(2)
Image saved successfully in Sdcard/XXX/Wallpapers and if I run again files are != null and comes not in the if block...It seems that all is correct but in the file explorer I don't see the folder and picture! -.-Pilchard
what else you are able to see there in file explorer and also try to run it on your mobile/device definitely you will get itDated
M
0

You cannot see the folder/file in explorer? Maybe it is because the MediaScanner is active, but not adding your files. You can do this in your program or switch the Media Scanner of somewhere in your phone settings.

MediaScanner

Trigger MediaScanner

Morphine answered 15/11, 2012 at 10:6 Comment(0)
F
0

Apparently there is a known bug in MTP.

Issue 195362 All phones using MTP instead of USB Mass storage do not properly show the list of files when that phone is connected to a computer using a USB cable. Android apps running on the device also cannot see these files.

It is actually as old as 2012

Fourierism answered 9/2, 2016 at 1:10 Comment(0)
M
0

I've encountered the same problem: created files and folders don't show immediately after being written to sdcard, despite the file being flushed and closed !!

They don't show on your computer over USB or a file explorer on the phone.

I observed three things:

  1. if the absolute path of the file starts with /storage/emulated/0/ it doesn't mean it'll be on your sdcard - it could be on your main storage instead.
  2. if you wait around 5 minutes, the files do begin to show over USB (i.e. Windows explorer and built-in file explorer)
  3. if you use adb shell ls /sdcard from terminal, then the file does show! you could use adb pull ... to get the file immediately. You could probably use DDMS too.

Code I used was:

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(myArrayList);

        try {
            File externalDir = getExternalStorageDirectory();
            File newFile = new File(externalDir, "myfile.txt");
            FileOutputStream os = new FileOutputStream(newFile);
            os.write(json.getBytes());
            os.flush();
            os.close();
            Timber.i("saved file to %s",newFile.getAbsoluteFile().toString());
        }catch (Exception ex)
        {
            Toast.makeText(getApplicationContext(), "Save to private external storage failed. Error message is " + ex.getMessage(), Toast.LENGTH_LONG).show();
        }

and

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(myArrayList);

        try {
            File externalDir = getExternalStorageDirectory();
            File newFile = new File(externalDir, "myfile.txt");
            FileWriter fw = new FileWriter(newFile);
            fw.write(json);
            fw.flush();
            fw.close();
            Timber.i("saved file to %s",newFile.getAbsoluteFile().toString());
        }catch (Exception ex)
        {
            Toast.makeText(getApplicationContext(), "Save to private external storage failed. Error message is " + ex.getMessage(), Toast.LENGTH_LONG).show();
        }

why is it like this? Seems like another one of those "Android-isms" that you have to suffer through the first time you experience it.

Mor answered 9/8, 2018 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.