如何从sdcard文件导入我的Android应用程序?

时间:2015-04-28 18:41:47

标签: android database sqlite fileinputstream

我也有很多记录数据库 - 我不想输入连续数据, 所以我想备份我的数据库然后然后我想将它导回到应用程序

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

我的代码数据库添加进程:

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

1 个答案:

答案 0 :(得分:0)

final String database_name =“beo”; //真;

final String database_name =“beo.db”; //假

我还添加manifest.xml ==>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>