2011-12-07 15 views
0

画像uriをカメラやギャラリーからSQLiteに保存します。 そして、私はSQLiteからuriというイメージを表示したいと思います。 もし私がこれをやりたければ、誰かが画像URLをSQLiteにバイトとして保存しなければならないと言いました。 imageViewに画像を設定することができます。 私はこの理論を理解していますが、まだ私のコーディングに悩まされています。 本当の場合、私はフォーマットされたイメージをsdcardまたはどこかに保存します。 誰かがBitmapFactorydecodeResourceと言っています。 とからuriを呼び出してください。 しかし、画像をR.drawableフォルダに保存する方法はわかりません。 私を助けることができますか?私はあなたに私のコーディングをいくつか与えます。 私はSQLiteにイメージを保存してロードする方法と、2週間でそれを変更する方法と戦っています!Androidはファイルシステムを使用してカメラやギャラリーから撮影した画像を保存します

本当に長いコーディングで申し訳ありません。私は今どこにいるのか分からない。 ありがとうございます。

fridgeDetails.java

populateFields(); 

private void populateFields() 
{ 
    if (mRowId != null) 
    { 
     Cursor data = mDbHelper.fetchItem(mRowId); 
     startManagingCursor(data); 
     //load image from sqlite 
     byte[] blob = data.getBlob(data.getColumnIndexOrThrow(FridgeDbAdapter.KEY_IMAGE)); 
     mImageView.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0, blob.length)); 
     nameEdit.setText(data.getString(data.getColumnIndexOrThrow(FridgeDbAdapter.KEY_NAME))); 
     categoryEdit.setText(data.getString(data.getColumnIndexOrThrow(FridgeDbAdapter.KEY_CATEGORY))); 
     expired_Date_Btn.setText(data.getString(data.getColumnIndexOrThrow(FridgeDbAdapter.KEY_EXPIRED_DATE))); 
    } 
    else{ 
    expired_Date_Btn.setText(
      new StringBuilder() 
      .append(mDay).append("/") 
      //month is 0 based. Then add 1 
      .append(mMonth + 1).append("/") 
      .append(mYear).append(" "));  
    } 

} 
    //create dialog for taking image 
    ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items); 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Select Image"); 

    builder.setAdapter(adapter, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) 
     { 
      if(item==0) 
      { 
       Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       try 
       { 
        cameraIntent.putExtra("return-data", true); 
        startActivityForResult(cameraIntent, PICK_FROM_CAMERA); 
       } 
       catch(ActivityNotFoundException e) 
       { 
        e.printStackTrace(); 
       }     
      } 
      else 
      { 
       Intent galleryIntent = new Intent(); 
       galleryIntent.setType("image/*"); 
       galleryIntent.setAction(Intent.ACTION_GET_CONTENT); 
       //image chooser 
       startActivityForResult(Intent.createChooser(galleryIntent, "Complete action using"), PICK_FROM_GALLERY); 
      } 
     } 
    }); 

confirmButton.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) { 
      //set alarm with expiration date     
      am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
      setOneTimeAlarm(); 
      Toast.makeText(fridgeDetails.this, "Alarm automatic set", Toast.LENGTH_SHORT).show(); 
      saveState(); 
      setResult(RESULT_OK); 
      finish(); 
     } 

    }); 

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode != RESULT_OK) return; 
     switch (requestCode) 
     { 
     case PICK_FROM_CAMERA: 
      Bundle extras = data.getExtras(); 
      Bitmap selectedImage = (Bitmap) extras.get("data"); 
      selectedImage = Bitmap.createScaledBitmap(selectedImage, 200, 200, false); 
      mImageView.setImageBitmap(selectedImage); 
      break; 

     case PICK_FROM_GALLERY: 
      Uri selectedImageUri = data.getData(); 
      selectedImagePath = getPath(selectedImageUri); 
      Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath); 
      Bitmap bt=Bitmap.createScaledBitmap(bitmap, 200, 200, false); 
      mImageView.setImageBitmap(bt); 
     break; 
     } 
} 

protected void onSaveInstanceState(Bundle outState) 
{ 
    super.onSaveInstanceState(outState); 
    saveState(); 
} 

@Override 
protected void onPause() 
{ 
    super.onPause(); 
    saveState(); 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    populateFields(); 
} 

private void saveState() 
{ 
    String name = (String) nameEdit.getText().toString(); 
    String category = (String) categoryEdit.getText().toString(); 
    String expired_date = (String) expired_Date_Btn.getText().toString(); 
    byte[] image = ConvertDrawableToByteArray(mImageView.getDrawable()); 
    if(mRowId == null) 
    { 
     long id = mDbHelper.insertItem(category, name, expired_date, image); 

     if(id>0) 
     { 
      mRowId = id; 
     }   
    } 
    else 
    { 
     mDbHelper.updateItem(mRowId, category, name, expired_date, image); 
    } 
} 

public static byte[] ConvertDrawableToByteArray(Drawable drawableResource) { 
    Bitmap imageBitmap = ((BitmapDrawable) drawableResource).getBitmap(); 
    ByteArrayOutputStream imageByteStream = new ByteArrayOutputStream(); 
    imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, imageByteStream); 
    byte[] imageByteData = imageByteStream.toByteArray(); 
    return imageByteData; 
} 

答えて

0

Get the URI of Captured image

にチェックこれをInserting image to DB as BLOB and Retrieve it back and display.

のためにこれを確認してくださいそれはちょうどあなたの要件ごとにそれと変更、サーバーからのダウンロードの画像が含まれており、DBに挿入します。

+0

がwholee1 @あなたは私のポストを確認しました - あなたが他の場所で保存したい場合は、follコード参照の詳細については

// create Intent to take a picture and return control to the calling application Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name // start the image capture Intent startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) 

のように場所を渡しますか? – Venky

1

イメージファイルの場所は、OnActivityResultメソッドのdata.getData()から取得できます。場所はSqliteの文字列として保存できます。次に、

imageView.setImageBitmap(BitmapFactory.decodeFile(filename) 

を使用してください。

デフォルトの場所はあなたのアプリケーションフォルダです。 http://developer.android.com/guide/topics/media/camera.html

関連する問題