mardi 28 avril 2015

how can i import from sdcard file to my application in android?

/I also have many records database- I do not want to enter continuous data, so I want to backup my database and then and then I want to import it back to the application/

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_exp= (Button) findViewById(R.id.btn_exp);
        btn_imp= (Button) findViewById(R.id.btn_imp);
        vt =new VeriTabani(this);

/*my code database add process */

        btn_exp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                export();
            }
        });
        btn_imp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                immport();
            }
        });


    }

    private void export(){   //funct. from application to sdcard file
        final String database_name="beo.db";
        final String sd_path= Environment.getExternalStorageDirectory().toString();
        final String sd_filePath=sd_path +"/buketecrinozturk/Backup/";
        final String sd_dbfilePath =sd_filePath +database_name;
        File file_databasePath =(getDatabasePath(database_name));
        File exp_sdfilePath = new File(sd_filePath);
        File exp_sddbfilePath = new File(sd_dbfilePath);
        if(!exp_sdfilePath.exists()){
            exp_sdfilePath.mkdirs();
        }

        Toast.makeText(getApplicationContext(), "Path is: "+exp_sdfilePath, Toast.LENGTH_LONG).show();
        try{
    //        exp_sddbfilePath.createNewFile();
            copyFile(file_databasePath,exp_sddbfilePath); // don't work
        }
        catch(IOException e){
            e.printStackTrace();

        }
    }
        private void immport(){ // funct. from sdcard file to my application
            final String database_name="beo.db";
            final String paket_ismi="com.example.beo.vt_importexport";
            final File sd_path=Environment.getExternalStorageDirectory();///mnt/sdcard
            final File data = Environment.getDataDirectory();///data
            final String sd_filePath="/buketecrinozturk/Backup";
            final String sd_dbfilePath =sd_path +sd_filePath +database_name;
            Log.d("vtImport","sd_dbfilePath" +sd_dbfilePath);

            try {
                if (sd_path.canWrite()) {
                    String  database_path= "/data/" + paket_ismi + "/databases/" + database_name;
                    File  imp_DBpath= new File(data, database_path);
                    File currentDB  = new File(sd_dbfilePath);
                    if (currentDB.exists()) {
                       copyFile(currentDB,imp_DBpath);
                        FileInputStream fIn = new FileInputStream(currentDB);
                        Toast.makeText(getBaseContext(), aBuffer,Toast.LENGTH_LONG).show();
                        Log.d("","aBuffer=>" + aBuffer);
                        myReader.close();
                    }
                    else
                        Toast.makeText(getBaseContext(), imp_DBpath.toString(),Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {

                Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                        .show();

            }
        }
        private  void copyFile(File src,File dst) throws IOException{
            FileInputStream inStream = new FileInputStream(src); // don't work !!
            Log.d("copyFile()","inStream_Path is:" +inStream);
            FileOutputStream outStream = new FileOutputStream(dst);
    //        FileOutputStream outStream = openFileOutput(dsta,Context.MODE_PRIVATE);
            Log.d("copyFile()","outStream_Path is:" +outStream.toString());
            FileChannel inChannel = inStream.getChannel();
            Log.d("copyFile()","inChannel_Path is:" +inChannel.toString());
            FileChannel outChannel = outStream.getChannel();
            Log.d("copyFile()","outChannel_Path is:" +outChannel.toString());

            try{
                inChannel.transferTo(0, inChannel.size(), outChannel);
                Log.d("copyFile()","try is:");
    //transfer bytes from the inputfile to the outputfile

                Log.d("copyFile()","while çalıştı:");
            }catch (IOException e){
                Log.d("copyFile()","hata=>"+e.toString());
            }finally{
                if(inStream!=null)
                    inStream.close();
                if(outStream!=null)
                    outStream.close();
            }
        }

Aucun commentaire:

Enregistrer un commentaire