2014-08-30 95 views
0

我想创建一个应用程序,用户可以从库中选择他们的个人资料图片。我决定将他们的个人资料照片保存为我的数据库Blob。我可以保存图像,甚至检索它。问题是,我无法替换它,或者每当我再次点击它,应用程序停止工作,并且当我检查我的表格存储图像时,它会显示“返回的数据过多...”太多的数据返回sqlite数据库的警告

public class AccountFragment extends Fragment implements OnClickListener { 
private LoginDataBaseAdapter loginDataBaseAdapter; 
Bitmap image; 
Bitmap bitmap; 
String picture_location; 
TextView textTargetUri; 
ImageView targetImage; 
public static final String MyPREFERENCES = "MyPrefs" ; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // create a instance of SQLite Database 
     loginDataBaseAdapter = new LoginDataBaseAdapter(getActivity()); 
     loginDataBaseAdapter=loginDataBaseAdapter.open(); 

     //intialize variables 

      textTargetUri = (TextView) rootView.findViewById(R.id.targeturi); 

      targetImage=(ImageView) rootView.findViewById(R.id.profpic); 

      targetImage.setOnClickListener(new ImageView.OnClickListener(){ 
      @Override 
      public void onClick(View arg0) { 
       Intent intent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(intent, 0); 
      }}); 



      showpic(); 


     return rootView; 


     } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode == Activity.RESULT_OK){ 
      Uri targetUri = data.getData(); 
      picture_location = targetUri.toString(); 
      textTargetUri.setText(targetUri.toString()); 
      Bitmap bitmap; 
      try { 

       bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(targetUri)); 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
       byte[] byteArray = stream.toByteArray(); 
       loginDataBaseAdapter.insertPhoto(byteArray); 
       showpic(); 


      } 
      catch (FileNotFoundException e){ 

       e.printStackTrace(); 
      } 

     } 
    } 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 

    } 

    public void showpic() { 



     Cursor cursor = loginDataBaseAdapter.fetchProfileImageFromDatabase(); 

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


        byte[] data = cursor.getBlob(cursor.getColumnIndex("Path")); 
        ByteArrayInputStream imageStream = new ByteArrayInputStream(data); 
        Bitmap theImage = BitmapFactory.decodeStream(imageStream); 
        targetImage.setImageBitmap(theImage); 

       } 




      cursor.close(); 
     } 
    } 

和我的数据库处理程序:

 //IMAGE 
      public static final String Profpic_TABLE = "ProfilePic"; 
      public static final String KEY_ProfpicID = "_id"; 
      public static final String KEY_ProfPic = "Path"; 


    //ProfilePic-Table 
      static final String DATABASE_ProfPic = 
        "create table " + Profpic_TABLE + " (" 
        + KEY_ProfpicID + " integer primary key DEFAULT 1, " 
        + KEY_ProfPic + " BLOB);"; 


     public long insertPhoto(byte[] EImage) { 

     db.execSQL("delete from "+ Profpic_TABLE); 

     try { 
      System.out.println("Function call : "); 
      ContentValues values = new ContentValues(); 

      values.put(KEY_ProfPic, EImage); 
      return db.insert(Profpic_TABLE, null, values); 


     } catch (Exception e) { 
      e.printStackTrace(); 
      return 0; 
     } 
    } 

     public Cursor fetchProfileImageFromDatabase() 
     { 
      return db.rawQuery("SELECT Path FROM ProfilePic where _id = 1 " , null); 
     } 

    } 

回答

0

我能够最终解决这个问题。事实证明,我必须这样做,而不是这样:

 public void showpic() { 

     LoginDataBaseAdapter db = loginDataBaseAdapter.open(); 
     boolean emptytab = false; 
     boolean empty = db.checkPic(null, emptytab); 

     //Cursor cursor = loginDataBaseAdapter.fetchProfileImageFromDatabase(); 

     if(empty==false) 
     { 
      Cursor cursor = loginDataBaseAdapter.fetchProfileImageFromDatabase(); 
      cursor.moveToFirst(); 
        byte[] data = cursor.getBlob(cursor.getColumnIndex("Path")); 
        ByteArrayInputStream imageStream = new ByteArrayInputStream(data); 
        Bitmap theImage = BitmapFactory.decodeStream(imageStream); 
        targetImage.setImageBitmap(theImage); 
      cursor.close(); 
       } 





     } 

和我的数据库适配器添加此:

公共布尔checkPic(字符串数,Object对象){

 boolean empty = false; 
     Cursor cursor = db.rawQuery("SELECT count(*) FROM ProfilePic", null); 

     if(cursor != null) 
     { 
      cursor.moveToFirst(); 
      if(cursor.getInt(0)== 0) 
      { 
       empty = true; //rows not null; 
      } 
      else 
      { 
       empty = false; // rows null; 
      } 
     } 
     return empty; 
    }