Milan Dhameliya
Milan Dhameliya

Reputation: 129

Need file save dialog for android

I need file save dialog for android, to save my app generated png files.

I tried Intent intentFileSave = new Intent(Intent.ACTION_PICK);

But this didn't work. In case, I don't have any file manager, Is android have default "save as" dialog ??

I also tried to integrate OI File Manager in my application. But I don't know, is the license of OI File Manager permits to use it as a library.

Is there any other option to ask user to pick location and name of the file ??

Upvotes: 2

Views: 9627

Answers (3)

Austin B
Austin B

Reputation: 1600

The OI File Manager doesn't need a license to use it because it's a self-contained piece of software. Think of the Open Intents project as an API of sorts. APIs don't need licenses, as far as I know.

As for saving a file, unfortunately the OI File Manager doesn't give you a "Save As" option, but you can easily work around that! Just have the user input the file name they want in your app, then use the PICK_DIRECTORY intent to let them tell you where to put it!

Upvotes: 0

Milan Dhameliya
Milan Dhameliya

Reputation: 129

I also tried,

intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(startDir, "vnd.android.cursor.dir/*");

But It shows me every possible intents such as FileManager, Gallery, Music, Contects, etc...
I didn't find any way the android decides possible intents that support "Save As" feature.

So, I have following solution. Here, I just need to add more and more tries for different file explorers that support Save As feature.

int iLoop = 0;
int iTryCount = 2;
for (iLoop = 0; iLoop < iTryCount; iLoop++)
{
    Intent intent = null;

    if (iLoop == 0)
    {
        // OI File Manager
        intent = new Intent(Intent.ACTION_PICK);
        intent.setData(startDir);
        intent.setAction("org.openintents.action.PICK_FILE");
        intent.putExtra("org.openintents.extra.TITLE", "Save As...");
    }
    else if (iLoop == 1)
    {
        // AndExplorer
        intent = new Intent(Intent.ACTION_PICK);
        intent.setDataAndType(startDir,
                "vnd.android.cursor.dir/lysesoft.andexplorer.file");
        intent.putExtra("explorer_title", "Save As...");
        intent.putExtra("browser_filter_extension_whitelist", "*.png");
        intent.putExtra("browser_line", "enabled");
        intent.putExtra("browser_line_textfield", "file1.png");
    }

    if (intent != null)
    {
        try
        {
            // Try to launch activity
            startActivityForResult(intent, REQUEST_SAVE_FILE);

            // TODO: Remove this notification on Publish
            Toast.makeText(m_baseSurfaceView.getContext(),
                    "Try : " + iLoop, Toast.LENGTH_SHORT).show();

            // If everything gone well, then break from here
            // otherwise see catch(..) block
            break;
        }
        catch (Exception e)
        {
            e.printStackTrace();

            // If all tries are done, then conclusion is that,
            // the user needs to install some file-manager application
            // to locate where to save the file
            if (iLoop == iTryCount - 1)
            {
                Toast.makeText(
                        m_baseSurfaceView.getContext(),
                        "Please Install some File Manager"
                                + " application to locate destination.",
                        Toast.LENGTH_LONG).show();
            }
        }
    }
}

Please comment if any suggestions.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007533

Is android have default "save as" dialog ??

No, mostly because apps either save the files in their app's portion of internal or external storage, or save them in the specific public directories named in the Environment class. They then use ACTION_SEND to share files with other services that can consume them.

But I don't know, is the license of OI File Manager permits to use it as a library.

I do not see any sign of a license in their GitHub repository for the source, which is unfortunate.

Is there any other option to ask user to pick location and name of the file ?

Beyond not doing it at all (and saving to standard locations), you can:

  • Link to file manager apps like OI File Manager via their specific Intent actions (e.g., org.openintents.action.PICK_DIRECTORY), prompting the user to install one if there is none

  • Poke around for some other library for choosing a directory and/or file

  • Create your own

Upvotes: 3

Related Questions