复制文件,将应用程序打开到应用程序中的某个临时文件夹中

时间:2013-01-03 11:08:50

标签: android android-intent

我使用自己的扩展程序“.swp”将文件与我的应用相关联。当用户尝试使用该扩展名打开文件时,我可以选择我的应用程序。对于例如如果在SD卡中的任何地方有文件“file1.swp”,并且如果用户点击该文件,我的应用程序也将在选项列表中可用于打开该应用程序。但是我想在选择我的应用程序时将“file1.swp”保存在某个临时文件夹中。

请提出任何建议............

1 个答案:

答案 0 :(得分:2)

我在下面发布我的整个代码,以便其他有相同问题的人可以参考:

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();

    }