Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: Save files in internal storage in a directory publicly

Tags:

android

I am trying to save images in a directory inside app file dir publicly. By publicly, I mean that I will be able to use action view intent on it.

/** Saves the bitmap to app storage */
    public int SaveBitmap(Bitmap bitmap, String filename, Boolean scan){

        // Save the file
        OutputStream os = null;

        String path = context.getFilesDir() + "/MyAppName/";
        File file = new File(path);
        if(!file.isDirectory())file.mkdirs();
        path += (filename + ".png");
        try {
            os = new BufferedOutputStream(new FileOutputStream(path,true));
            //os = mContext.openFileOutput(, Context.MODE_PRIVATE);
            bitmap.compress(CompressFormat.PNG, 100, os);
            os.flush();
            os.close();
            bitmap.recycle();
        } catch (IOException e) {
            e.printStackTrace();
            return 1; //Failure
        }

        File fileR = new File(path);

        fileR.setReadable(true, false);
        // Show in Gallery
        if(scan) ShowInGallery(filename);

        return 0; // SUCCESS

    }

This piece of code doesn't do the job, the files are still created in private mode.

Using:

context.openFileOutput(filename + ".png", Context.MODE_WORLD_READABLE); 

I was able to open them publicly, but I cannot save the file in a directory in that.

Note that I am targetting users that do not have SD cards, that is why I am not using External Storage.

What can I do?

like image 297
zed Avatar asked Feb 06 '14 18:02

zed


2 Answers

You're going to want to use Environment.getExternalStoragePublicDirectory (String type) or Environment.getExternalStorageDirectory as the root of your file directory. See the documentation for more details.

Edit:

From the Environment documentation under getExternalStorageDirectory ():

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

All "External" means is that it's external to your application, or public. So, if an SD card is available, then this method defaults to that storage directory. If the device doesn't use an SD card, the device's build-in storage is used.

like image 122
Submersed Avatar answered Sep 27 '22 23:09

Submersed


I think that the safest thing to do in my case is to use Environment.getExternalStoragePublicDirectory (String type) And if an exception is caught, I will save it to internal storage using normal getFilesDir. I haven't found a way to make that folder inside app data readable publicly.

This is because getExternalStoragePublicDirectory may fail if the device should have an SD card but it is not mounted. So you can't depend on it.

like image 25
zed Avatar answered Sep 27 '22 21:09

zed