em có đoạn code như này
public class MainActivity extends AppCompatActivity {
String DATABASE_NAME="Song.sqlite";
String DB_PATH_SUFFIX = "/databases/";
SQLiteDatabase database=null;
private void CopyDataBaseFromAsset() {
try
{
InputStream myInput=getAssets().open(DATABASE_NAME);
String outFileName=layDuongDanLuutru();
File f = new File(getApplicationInfo().dataDir + DB_PATH_SUFFIX);
if(!f.exists())// nếu ko tồn tại thì tạo mới mà có rồi thì thôi
{
f.mkdir();
}
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}catch (Exception ex)
{
Log.e("loi sao chep",ex.toString());
}
}
private void xuLySaoChep() {// hàm này dùng để copy sqllite từ assets vào hệ thông
File dbFile = getDatabasePath(DATABASE_NAME);
if (!dbFile.exists())
{
try
{
CopyDataBaseFromAsset();
Toast.makeText(this, "Copying sucess from Assets folder", Toast.LENGTH_LONG).show();
}catch(Exception ex)
{
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
}
}
em có hai câu hỏi muốn hỏi là làm sao mình tạo ra một thứ gì đó .Ở mọi dự án mình làm có thể dùng luôn hay hàm này chứ ko phải copy paste vào .Thứ hai là có cách nào viết tối ưu hơn ko ạ .Chúc tất cả anh em trên diễn đàn một kỳ nghỉ vui vẻ