1

I want to save an image in an ImageView to the database using R.id not R.drawable because the image can change(because its a camera picture). l can not use the path to the image to save because the gallery might be tampered around with.

HotOrNot entry = new HotOrNot(this);  
byte[] image4 = HotOrNot.getBytes(BitmapFactory.decodeResource(null, R.id.imageView2));
entry.open();
entry.createEntry(image4);
entry.close();

public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • when i use R.id.imageView2 it gives me errors but if i use a sample r.drawable.image it works but l want to use R.id.imageview2 – Creative Skillz Mar 28 '13 at 08:15
  • with BitmapFactory.decodeResource u only can use actual image from the drawable. how about creating a bitmap and then convert it to drawable ? ? – Kosh Mar 28 '13 at 08:17
  • R.id and R.drawable values can also change since they are generated. You should save the image itself, for example using Base64 encoding. – Egor Mar 28 '13 at 08:19
  • possible duplicate of [Android How to save camera images in database and display another activity in list view?](http://stackoverflow.com/questions/9941637/android-how-to-save-camera-images-in-database-and-display-another-activity-in-li) – gabrielhilal Mar 28 '13 at 12:35

2 Answers2

1

You need to understand that, R.drawable.image is the ID of the actual image. And R.id.imageView2 is the ID of the ImageView.

Your argument is that the image you want to store can change. So in that case, what you can do is use yourImageView.getDrawable(), so that you will get the current image in the ImageView and then convert it to Bitmap using this

BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
byte[] image4 = HotOrNot.getBytes(bitmap); // Your code from here.
Rahul
  • 44,383
  • 11
  • 84
  • 103
0

Whatever is represented by R.id.. is an id for a resource type from the project's pre compiled assets. I don't think you can get an id value for an image taken with the Camera.

Also note that the type of R.id.. is an int.

I know the above is not an answer. If you want to take an image using the Camera and save it in the database, check this question in SO. Hope it helps.

Community
  • 1
  • 1
midhunhk
  • 5,560
  • 7
  • 52
  • 83
  • ok,so how can i save the image in the imageview to the database because i am taking a picture and insert it into a imageView and from the imageView to the database. – Creative Skillz Mar 28 '13 at 08:29