从/文件浏览器资产/数据/数据​​文件夹拷贝数据库文件 - 机器人数据、机器人、文件夹、数据库文件

2023-09-04 07:03:29 作者:謌習慣ろ分手

我在从/资产拷贝数据库文件到/ data / data文件夹中的文件浏览器的问题。我已搜查这个网站,发现很多答案,但无法找到一个适合我的背景下,合适的答案。我创建了数据库外的SQLite Manager和导入到资产的文件夹中。现在,当我运行我的应用程序,我越来越NullPointerException异常的模拟器。我发现包没有在/数据/数据​​文件夹中创建的。但是,应用程序启动的仿真器。调试器还没有显示出任何错误。

I have issues in copying database file from /assets to /data/data folder in file explorer. I have searched this website, found many answers, but could not find the appropriate answer that suits my context. I have created the database externally with SQLite Manager and imported it into assets folder. Now, when I run my application, I am getting NullPointerException in the emulator. I found that the package has not created in /data/data folder at all. But the application is launching in the emulator. The debugger also did not show any errors.

我曾尝试以下解决方案 -

I have tried the following solutions -

而重新启动Eclipse和仿真器,删除并重新创建现有的仿真器,最后重新启动笔记本电脑

Restarted eclipse and emulator, deleted and recreated existing emulator, finally restarted laptop

无解的解决我的问题。任何一个可以请你告诉我,什么我的错误?

None of the solutions solved my problem. Can any one please tell me, whats my mistake?

下面是我的MainActivity.java: -

Here is my MainActivity.java :-

public class MainActivity extends Activity implements OnClickListener
{
Dialog d;
private photoDbAdapters mDbAdapter;
public int currentimageindex=0;
 String[] sp;   

int p=1;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        images();
 }
 public void images()
 {
        try{
            String rt=mDbAdapter.fetchsingles(); //the application 
                                           is getting crashed in this part

        int id = getResources().getIdentifier(rt, null, null);
        ImageView iv = (ImageView) findViewById(R.id.ImageView3_Left);
        iv.setImageResource(id);
    }catch(NullPointerException er)
{
        String ht=er.toString();
        Toast.makeText(getApplicationContext(), ht, Toast.LENGTH_LONG).show    ();
    }}


@Override
public void onClick(View v)
{
    finish();
    android.os.Process.killProcess(android.os.Process.myPid());
    // TODO Auto-generated method stub
}

fetchsingles方法: - (此方法将retreive从数据库映像文件名)

fetchsingles method :- (this method will retreive image file name from database)

public String fetchsingles()
{

    try{

img = mDbHelper.getData();


    }catch(Exception e)
    {
        String error= e.toString();
        Dialog d = new Dialog(null);
        d.setTitle("image cannot be fetched");
        int err=Integer.parseInt(error);
        d.setContentView(err);
        d.show();
    }
    return img;


}

GetData方法: -

getdata method:-

public String getData() 
{
    // TODO Auto-generated method stub

    String dry="SELECT "+COL_DATA+" FROM Photos WHERE "+COL_ID+"=2;";

        Cursor c = myDataBase.rawQuery(dry,null);

        String result = "";
        int img = c.getColumnIndex(COL_DATA);


            result = c.getString(img);





    return dry;

在code看起来冗长,不介意任何事情,请大家帮我解决这个问题。

The code might look lengthy, don't mind anything and please help me in solving this problem.

在此先感谢。

推荐答案

尝试使用以下code从资产数据/数据​​/包目录复制数据库

Try using below code to copy database from assets to data/data/package directory

package com.example.myapp;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

      public class DataBaseHelper1 extends SQLiteOpenHelper{
      private static String DB_PATH = "/data/data/com.example.myapp/databases/";

      private static String DB_NAME = "myDB.sqlite";

      private SQLiteDatabase myDataBase;

      private final Context myContext;

      public DataBaseHelper1(Context context) 
      {
          super(context, DB_NAME, null, 1);
          this.myContext = context;
      }

      public void createDataBase() throws IOException{


    boolean dbExist = checkDataBase();

      if(dbExist)
      {
          Log.i("DB....", "database available....");
      }
      else
      {
          this.getWritableDatabase();

          try {

          copyDataBase();

          } catch (IOException e) {

          throw new Error("Error copying database");

          }

         Log.i("DB..", "database created.....");
       }   

      }


      public boolean checkDataBase(){

      SQLiteDatabase checkDB = null;

      try{

      String myPath = DB_PATH + DB_NAME;

      checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

      }catch(SQLiteException e){

          Log.e("CheckDb","DB not found");
      //database does't exist yet.

      if(checkDB != null){

      checkDB.close();

      }
      }
      finally
      {
          if(checkDB != null){

              checkDB.close();

              } 
          this.close();
      }
      return checkDB != null ? true : false;

      }




      private void copyDataBase() throws IOException{

      InputStream myInput = myContext.getAssets().open(DB_NAME);

      String outFileName = DB_PATH + DB_NAME;

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

      }

      public SQLiteDatabase openDataBase() throws SQLException{

      String myPath = DB_PATH + DB_NAME;

      return myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

      }


      @Override

      public synchronized void close() {

      if(myDataBase != null)

      myDataBase.close();

      super.close();

      }

      @Override

      public void onCreate(SQLiteDatabase db) {

      }

      @Override

      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


      }  


      public void getData()
      {
          SQLiteDatabase myDB ;
          Cursor cursor ;
           try {

                myDB=this.openDataBase();                   

                    cursor=myDB.rawQuery("SELECT * FROM Country_Master",null);


                    if (cursor != null ) {
                       if  (cursor.moveToFirst()) {
                       do {

                           // put your code to get data from cursor                      

                       }while (cursor.moveToNext());
                       }

                    }



                   if(cursor != null)
                    {
                        myDB.close();
                       cursor.close();
                    }                      
                    }catch(SQLException sqle){

                    throw sqle;

                    }


        }
}

也把这个code下onCreate方法在你的活动,你要复制的数据库。

Also put this code under onCreate method in your activity where you want to copy database..

 DataBaseHelper1 myDbHelper = new DataBaseHelper1(MyActivity.this); 
     try 
     {
         myDbHelper.createDataBase();
     }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }

     finally
     {
         myDbHelper.close();
     }