2017-03-13 49 views
-2

我是Android開發新手。 我正在嘗試使用SQlite創建Android應用程序。我在Blob中插入圖像時遇到問題。文字很容易插入,但問題與圖像有關。我知道我需要使用Blob,但問題是,在哪裏使用它?我需要在我的佈局中使用Imageview? 我需要點擊圖片按鈕,然後插入圖片從SD卡或手機,然後它保存到Sqlite數據庫與文本。之後,從Sqlite數據庫中進行回顧。如何在嘗試將圖像插入Sqlite數據庫時使用Blob?

感謝您的關注和幫助。

有我的代碼: MainActivity

public class MainActivity extends AppCompatActivity { 
EditText etName,etRoll,etAddress,etBranch,etEmail,etImage; 
Button btnSubmit,btngetdata,btndroptable; 
DatabaseHelpher helpher; 
List<DatabaseModel> dbList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    dbList= new ArrayList<DatabaseModel>(); 
    etName = (EditText)findViewById(R.id.etName); 
    etRoll = (EditText)findViewById(R.id.etRoll); 
    etAddress =(EditText)findViewById(R.id.etAddress); 
    etBranch = (EditText)findViewById(R.id.etBranch); 
    etEmail = (EditText)findViewById(R.id.etEmail); 
    etImage = (Image)findViewById(R.id.image);  // ???? 
    btnSubmit =(Button)findViewById(R.id.btnSubmit); 
    btngetdata =(Button)findViewById(R.id.btngetdata); 

    btngetdata.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(MainActivity.this, SecondActivity.class)); 

      // startActivity(new Intent(MainActivity.this, DetailsActivity.class)); 

     } 
    }); 

    btnSubmit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      String name=etName.getText().toString(); 
      String email=etEmail.getText().toString(); 
      String roll=etRoll.getText().toString(); 
      String address=etAddress.getText().toString(); 
      String branch=etBranch.getText().toString(); 
      Blob image=etImage 

     if(name.equals("") || email.equals("") || roll.equals("") ||address.equals("")||branch.equals("")){ 
      Toast.makeText(MainActivity.this,"Please fill all the fields",Toast.LENGTH_LONG).show(); 
     }else { 
      helpher = new DatabaseHelpher(MainActivity.this); 
      helpher.insertIntoDB(name, email, roll, address, branch, image); 
     } 
      etName.setText(""); 
      etRoll.setText(""); 
      etAddress.setText(""); 
      etBranch.setText(""); 
      etEmail.setText(""); 


      Toast.makeText(MainActivity.this, "insert value", Toast.LENGTH_LONG); 

     } 
    }); 

}} 

DatabaseModel.java:

public class DatabaseModel { 
    private String name; 
    private String roll; 
    private String address; 
    private String branch; 
    private String email; 
    private String image; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getRoll() { 
     return roll; 
    } 

    public void setRoll(String roll) { 
     this.roll = roll; 
    } 

    public String getAddress() { 
     return address; 
    } 

    public void setAddress(String address) { 
     this.address = address; 
    } 

    public String getBranch() { 
     return branch; 
    } 

    public void setBranch(String branch) { 
     this.branch = branch; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getImage() { 
     return image; 
    } 

    public void setImage (byte[] image) { 
     this.image = image; 
    } 
} 

DatabaseHelper.java:

public class DatabaseHelpher extends SQLiteOpenHelper { 
    private static final String DATABASE_NAME="student"; 
    private static final int DATABASE_VERSION = 1; 
    private static final String STUDENT_TABLE = "stureg"; 
    private static final String STU_TABLE = "create table "+STUDENT_TABLE +"(name TEXT,email TEXT primary key,roll TEXT,address TEXT,branch TEXT,,image BLOB)"; 

    Context context; 

    public DatabaseHelpher(final Context context) { 
     super(context, Environment.getExternalStorageDirectory() 
       + File.separator + DATABASE_NAME, null, DATABASE_VERSION); 
     this.context = context; 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

     db.execSQL(STU_TABLE); 
    } 

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

     db.execSQL("DROP TABLE IF EXISTS " + STUDENT_TABLE); 

     // Create tables again 
     onCreate(db); 
    } 

    public void insertIntoDB(String name,String email,String roll,String address,String branch,byte[]image_data){ 
     Log.d("insert", "before insert"); 

     // 1. get reference to writable DB 
     SQLiteDatabase db = this.getWritableDatabase(); 

     // 2. create ContentValues to add key "column"/value 
     ContentValues values = new ContentValues(); 
     values.put("name", name); 
     values.put("email", email); 
     values.put("roll", roll); 
     values.put("address", address); 
     values.put("branch", branch); 
     values.put("image", image_data); 

     // 3. insert 
     db.insert(STUDENT_TABLE, null, values); 
     // 4. close 
     db.close(); 
     Toast.makeText(context, "insert value", Toast.LENGTH_LONG); 
     Log.i("insert into DB", "After insert"); 



    } 


    public List<DatabaseModel> getDataFromDB(){ 
     List<DatabaseModel> modelList = new ArrayList<DatabaseModel>(); 
     String query = "select * from "+STUDENT_TABLE; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(query,null); 
     if (cursor.moveToFirst()){ 
      do { 
       DatabaseModel model = new DatabaseModel(); 
       model.setName(cursor.getString(0)); 
       model.setEmail(cursor.getString(1)); 
       model.setRoll(cursor.getString(2)); 
       model.setAddress(cursor.getString(3)); 
       model.setBranch(cursor.getString(4)); 
       model.setImage(cursor.getBlob(5)); //Add byte paramter in your DatabaseModel 
       modelList.add(model); 
      }while (cursor.moveToNext()); 
     } 


     Log.d("student data", modelList.toString()); 


     return modelList; 
    } 



    public void deleteARow(String email){ 
     SQLiteDatabase db= this.getWritableDatabase(); 
     db.delete(STUDENT_TABLE, "email" + " = ?", new String[] { email }); 
     db.close(); 
    } 
} 

activity_main.layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical"> 
    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      app:layout_scrollFlags="scroll|enterAlways" /> 

    </android.support.design.widget.AppBarLayout> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Enter Email"/> 

       <EditText 
        android:id="@+id/etEmail" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Enter Branch"/> 
       <EditText 
        android:id="@+id/etBranch" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Enter Address"/> 
       <EditText 
        android:id="@+id/etAddress" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Enter Roll"/> 
       <EditText 
        android:id="@+id/etRoll" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Enter Name"/> 
       <EditText 
        android:id="@+id/etName" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 
       <Button 
        android:id="@+id/btnSubmit" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="uložit do databáze" 
        android:textColor="#ffffff" 
        android:background="@color/colorPrimary"/> 

      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginTop="10dp" 
       android:orientation="horizontal"> 

       <Button 
        android:id="@+id/btngetdata" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="Zobrazit seznam rumů" 
        android:textColor="#ffffff" 
        android:background="@color/colorPrimary"/> 

      </LinearLayout> 

     </LinearLayout> 
    </ScrollView> 


</LinearLayout> 

Activity_details.layout

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:layout_scrollFlags="scroll|enterAlways" /> 
</android.support.design.widget.AppBarLayout> 

<TextView 
    android:id="@+id/name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="name" 
    android:paddingTop="15dp" 
    android:paddingLeft="20dp" 
    android:textSize="36sp" /> 

<TextView 
    android:id="@+id/roll" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Roll"/> 
<TextView 
    android:id="@+id/address" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Address"/> 
<TextView 
    android:id="@+id/branch" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Branch"/> 
<TextView 
    android:id="@+id/email" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Email"/> 
<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/image"/> 
</LinearLayout> 

何處添加圖像視圖?當我嘗試進入佈局時,我不知道,在何處將代碼推入到java類中? 我需要幫忙,現在在哪裏找到如何去做。我需要在我的代碼中加入,因爲它是我的作業。請有人請幫助我。

感謝您在數據庫

回答

0

影像保存不好的做法(保存在文件夾更漂亮)。

但是...... 如何在字節轉換圖像視圖[]

image      = (ImageView)findViewById(R.id.qrcode); 
Bitmap bitmap    = ((BitmapDrawable)image.getDrawable()).getBitmap(); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); 
byte[] image=stream.toByteArray(); 

然後保存您圖像的byte []在數據庫中。

+0

感謝com。但是,在何處編寫此代碼,到類DatabaseHelper或MainActivity?和什麼寫入佈局?你的onClickListener中的 – zajicek

+0

用上面的算法創建一個變量byte [],並調用你的** helpher.insertIntoDB(name,email,roll,address,branch,imageByteArray); **你已經做了90%,只需要將字節[]傳遞給方法 –

+0

我在錯誤代碼中添加了answear,出了什麼問題? – zajicek

0

For @Peterr

這裏有什麼問題?

btnSubmit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     String name=etName.getText().toString(); 
     String email=etEmail.getText().toString(); 
     String roll=etRoll.getText().toString(); 
     String address=etAddress.getText().toString(); 
     String branch=etBranch.getText().toString(); 
     image = (ImageView)findViewById(R.id.image); //error on image???? 
     Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); //error on image ???? 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); 
     byte[] image=stream.toByteArray(); 


    if(name.equals("") || email.equals("") || roll.equals("") ||address.equals("")||branch.equals("")){ 
     Toast.makeText(MainActivity.this,"Please fill all the fields",Toast.LENGTH_LONG).show(); 
    }else { 
     helpher = new DatabaseHelpher(MainActivity.this); 
     helpher.insertIntoDB(name, email, roll, address, branch, imageByteArray); //red is ImageByteArray ???? 
    } 
     etName.setText(""); 
     etRoll.setText(""); 
     etAddress.setText(""); 
     etBranch.setText(""); 
     etEmail.setText(""); 


     Toast.makeText(MainActivity.this, "insert value", Toast.LENGTH_LONG); 

    } 
}); 
0
ImageView image = (ImageView)findViewById(R.id.image); 

是指涉您在XML佈局 ImageView的組件。

在您的第二行活動中,您聲明圖像EditText,這不是EditText,這是ImageView。

0

步驟:

  1. 獲得從圖像視圖的圖像。
  2. 將圖像轉換爲字節數組。
  3. 將字節數組作爲數據/參數傳遞給您的數據庫查詢
相關問題