nidhi
nidhi

Reputation: 763

Copy file which opens the app into some temp folder in the app

I am using my own extension ".swp" for associating files with my app. And I am able to get option of my app while user tries to open file with that extension. For e.g. If there is say file "file1.swp" anywhere in the sd card, and if user clicks on that file, my app will also be available in the options list for opening that app. But I want to save "file1.swp" in some temp folder at the time it selects my app to get opened.

Any suggestions please............

Upvotes: 0

Views: 920

Answers (1)

nidhi
nidhi

Reputation: 763

I am posting my whole code below so that others having same question can refer to:

String strFilename = "Not set";
        Intent intent = getIntent();
        if(intent != null)
        {
            Uri uri = intent.getData();
            if(uri != null)
            {
                strFilename = intent.getData().getPath();
                String NameOfFile = intent.getDataString();


                Toast.makeText(SplashActivity.this, "strFilename" + strFilename + "" + "Name of file is::" + NameOfFile, Toast.LENGTH_LONG).show();
                System.out.println("strFileName::"+ strFilename );

                System.out.println("NameOfFile::"+ NameOfFile );
                try {
                    copyFile(strFilename,"/mnt/sdcard/Profile.swp");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }else{

        }

        }





//copy file into other location
    private void copyFile(String assetFilename, String outFileName ) throws IOException{

        System.out.println("outFileName ::" + outFileName);
        //Open your local db as the input stream
        InputStream myInput = new FileInputStream(assetFilename);

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[2048];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

Upvotes: 2

Related Questions