2016-12-30 5 views
1

古いベースを特定のパスから新しいベースに置き換えたいと思います。私は古いバージョンを削除するだけで新しいファイルをコピーする必要がありますか?それは動作しますが、私は現金または再起動デバイスをクリアした直後にアプリケーションは新しいベースを適用します。なぜそうなのか?またはdbHelper.onUpgrade()を使用する必要がありますか?おそらくそれは良い。しかし、それはSQLiteDatabaseをparamとして必要とするので、私はonUpgrade()に引数を設定することができません、私はどのように必要な引数を設定することができますので、DBへのファイルパスがありますか?それはこのようなものになります。外部ファイルからDBを置き換える(上書きする)方法

downloadDbPath = file.getPath(); 
    sql = new SQLiteDatabase().getPath(downloadDbPath); 
    dbHelper.onUpgrade(sql, 1, 2); 

をし、それが誰かに興味深い、私は次のやった場合、私は、二行目Error: SQLiteDatabase() is not public in SQLiteDatabase; cannot be accessed from outside packageのエラーがだから私は

答えて

0

オーケー新しいオブジェクトを作成カントがあります。 メイン方法:

public void downloadDb() { 
     new FileChooser(this).setFileListener(new FileChooser.FileSelectedListener() { 
      @Override public void fileSelected(final File file) { 
       downloadDbPath = file.getPath(); 
       try { 
        InputStream in = new FileInputStream(downloadDbPath); 
        OutputStream out = new FileOutputStream(uploadDbPath); 

        // Transfer bytes from in to out 
        byte[] buf = new byte[1024]; 
        int len; 
        while ((len = in.read(buf)) > 0) { 
         out.write(buf, 0, len); 
        } 
        deleteCache(context); 
        restartApp(); 
        in.close(); 
        out.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }).showDialog(); 
    } 

付属品:

クリーンキャッシュ: https://stackoverflow.com/a/23908638/4540861

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

public static void deleteCache(Context context) { 
    try { 
     File dir = context.getCacheDir(); 
     deleteDir(dir); 
    } catch (Exception e) {} 
} 

public static boolean deleteDir(File dir) { 
    if (dir != null && dir.isDirectory()) { 
     String[] children = dir.list(); 
     for (int i = 0; i < children.length; i++) { 
      boolean success = deleteDir(new File(dir, children[i])); 
      if (!success) { 
       return false; 
      } 
     } 
     return dir.delete(); 
    } else if(dir!= null && dir.isFile()) { 
     return dir.delete(); 
    } else { 
     return false; 
    } 
} 

再起動アプリ: https://stackoverflow.com/a/17166729/4540861

public void restartApp(){ 
    Intent mStartActivity = new Intent(context, MainActivity.class); 
    int mPendingIntentId = 123456; 
    PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT); 
    AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); 
    System.exit(0); 
} 

チューザ: https://rogerkeays.com/simple-android-file-chooser

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

class FileChooser { 
    private static final String PARENT_DIR = ".."; 

    private final Activity activity; 
    private ListView list; 
    private Dialog dialog; 
    private File currentPath; 

    // filter on file extension 
    private String extension = null; 
    public void setExtension(String extension) { 
     this.extension = (extension == null) ? null : 
       extension.toLowerCase(); 
    } 

    // file selection event handling 
    public interface FileSelectedListener { 
     void fileSelected(File file); 
    } 

    public FileChooser setFileListener(FileSelectedListener fileListener) { 
     this.fileListener = fileListener; 
     return this; 
    } 

    private FileSelectedListener fileListener; 

    public FileChooser(Activity activity) { 
     this.activity = activity; 
     dialog = new Dialog(activity); 
     list = new ListView(activity); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override public void onItemClick(AdapterView<?> parent, View view, int which, long id) { 
       String fileChosen = (String) list.getItemAtPosition(which); 
       File chosenFile = getChosenFile(fileChosen); 
       if (chosenFile.isDirectory()) { 
        refresh(chosenFile); 
       } else { 
        if (fileListener != null) { 
         fileListener.fileSelected(chosenFile); 
        } 
        dialog.dismiss(); 
       } 
      } 
     }); 
     dialog.setContentView(list); 
     dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
     refresh(Environment.getExternalStorageDirectory()); 
    } 

    public void showDialog() { 
     dialog.show(); 
    } 


    /** 
    * Sort, filter and display the files for the given path. 
    */ 
    private void refresh(File path) { 
     this.currentPath = path; 
     if (path.exists()) { 
      File[] dirs = path.listFiles(new FileFilter() { 
       @Override public boolean accept(File file) { 
        return (file.isDirectory() && file.canRead()); 
       } 
      }); 
      File[] files = path.listFiles(new FileFilter() { 
       @Override public boolean accept(File file) { 
        if (!file.isDirectory()) { 
         if (!file.canRead()) { 
          return false; 
         } else if (extension == null) { 
          return true; 
         } else { 
          return file.getName().toLowerCase().endsWith(extension); 
         } 
        } else { 
         return false; 
        } 
       } 
      }); 

      // convert to an array 
      int i = 0; 
      String[] fileList; 
      if (path.getParentFile() == null) { 
       fileList = new String[dirs.length + files.length]; 
      } else { 
       fileList = new String[dirs.length + files.length + 1]; 
       fileList[i++] = PARENT_DIR; 
      } 
      Arrays.sort(dirs); 
      Arrays.sort(files); 
      for (File dir : dirs) { fileList[i++] = dir.getName(); } 
      for (File file : files) { fileList[i++] = file.getName(); } 

      // refresh the user interface 
      dialog.setTitle(currentPath.getPath()); 
      list.setAdapter(new ArrayAdapter(activity, 
        android.R.layout.simple_list_item_1, fileList) { 
       @Override public View getView(int pos, View view, ViewGroup parent) { 
        view = super.getView(pos, view, parent); 
        ((TextView) view).setSingleLine(true); 
        return view; 
       } 
      }); 
     } 
    } 


    /** 
    * Convert a relative filename into an actual File object. 
    */ 
    private File getChosenFile(String fileChosen) { 
     if (fileChosen.equals(PARENT_DIR)) { 
      return currentPath.getParentFile(); 
     } else { 
      return new File(currentPath, fileChosen); 
     } 
    } 
} 
関連する問題