2014-08-30 3 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

나는 마지막으로 그것을 해결할 수 있었다. 나는 그것을 대신 이런 식으로 할 필요가 밝혀 :

공공 부울 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; 
    } 
:
 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(); 
       } 





     } 

난이 추가 내 DB 어댑터

관련 문제