I have an ImageButton that is initially set to a drawable resource. During an Activity, I want to set the image to a bitmap of a user photo. The picture taking and saving work correctly, but the ImageButton's image does not change.
Here is my code:
Bitmap bitmap = BitmapFactory.decodeFile(PATH + "/image.jpg");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, stream);
Log.d(TAG, bitmap.toString()); //prints out android.graphics.Bitmap#41d772a8
rearEndImageButton.setImageBitmap(bitmap);
To do it form sd card you can try with..
Bitmap bmp = BitmapFactory.decodeFile("path_to_file");
ImageButton rearEndImageButton = (ImageButton)findViewById(R.id.rearEndImageButton);
rearEndImageButton.setImageBitmap(bmp);
In the activity onCreate method assign your button to a variable, and then set a resource or Bitmap which will be decoded from a file using the BitmapFactory class.
You can use File explorer to select an image from sd card and assign that image path to BitmapFactory.decodeFile().
Related
I have used imageview to capture & load images from gallery
But now I need to send that images to server using path of an image.can anyone help me in getting filepath of that image from imageview.
//First set imageview
imageview.buildDrawingCache();
//getBitmap from ImageView
Bitmap bm=imageview.getDrawingCache();
FileOutputStream out = new FileOutputStream(filePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, imagePath, name, description);
Presently I am designing an application based on photo editing. While doing this i encountered with a problem i.e.
I have gone through a tutorial "how to apply RGB color filter for an image" from this link and this tutorial is very helpful and nice.
But the problem is after applying RGB color filter to the image i need to save the changed image in sd card.
I have googled a lot for this but didn't found exact thing.
Many of them sugested to use paint() But im not getting how to use that.
So exactly my problem is "After Applying RBG Coloration to image I need to save that image in SD Card, But I do not found the how to do it" ?
How to Save an Android ImageView to SD Card
You have an ImageView which you've modified via various lighting effects and color filters and now you wish to save the result to the SD card as as a .jpg or .png format image.
Here's how:
Load Bitmap image from View.
Save Bitmap image to SD card.
Example:
Don't forget to test for Exceptions and add the necessary permissions to your manifest!
ImageView imageView = <View to save to SD card>;
Bitmap bitmap = loadBitmapFromView(imageView);
final String pathTxt = Environment.getExternalStorageDirectory();
File storagePath = new File(pathTxt);
File file = new File(storagePath, "filename.jpg");
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
private Bitmap loadBitmapFromView(View v) {
final int w = v.getWidth();
final int h = v.getHeight();
final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
final Canvas c = new Canvas(b);
//v.layout(0, 0, w, h);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
Theree are two methods for this...
1.after applying RGB values save those values in a variables and apply that values to the image selected.
2.after applying RGB values take the image from image view and save it
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();
}
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.
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.
I have a RelativeLayout with a loaded bitmap image using the Touch V2 example from Pragmatic Bookshelf -- http://media.pragprog.com/titles/eband3/code/Touchv2/src/org/example/touch/Touch.java
I've added a separate button with onclicklistener that when clicked will load an image from the gallery. On the activity result the image is loaded as a bitmap into the RelativeLayout:
public void getPictureFromFile(Uri targetUri){
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = scale(getContentResolver()
.openInputStream(targetUri));
workinprogress = BitmapFactory.decodeStream(
getContentResolver().openInputStream(targetUri),
null, options);
view.setImageBitmap(workinprogress);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
One the next button click, I grab the image of the relativelayout using:
thepicture.buildDrawingCache(true);
Bitmap bm = Bitmap.createBitmap(thepicture.getDrawingCache());
The process works terrific -- for the first image. When I load another image again, the bitmap passed is still the same as the original. I've tried the thepicture.invalidate() and thepicture.resetDrawableState() before getDrawingCache() but neither seem to update the image to the newly loaded picture, although the frame layout displays the correct image.
Is there something I don't understand about refreshing drawingCache that I need to implement for the second image I load?
To make it work more than once you have to use view.setDrawingCacheEnabled(true) each time before and view.setDrawingCacheEnabled(false) each time after calling view.getDrawingCache(). See the example:
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache(true);
File imageFile = new File(Environment.getExternalStorageDirectory(),
"Pictures/image.jpg");
FileOutputStream fileOutputStream = new FileOutputStream(imageFile);
imageView.getDrawingCache(true).compress(CompressFormat.JPEG, 100,
fileOutputStream);
fileOutputStream.close();
imageView.setDrawingCacheEnabled(false);
I've Image stored in ImageView and after several processes (flip, rotate, fix color, etc) it's saved back as new file. However, I just realize that when I just load into ImageView and soon after that directly save the result, I got different result. Take a look at attached image for reference.
image source
image result
Here's how I extract image from ImageView:
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + bufferPath;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
selectedImage.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(selectedImage.getDrawingCache());
selectedImage.setDrawingCacheEnabled(false);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
imageFile = new File( filePath );
//write the bytes in file
FileOutputStream fo = new FileOutputStream(imageFile);
fo.write(bytes.toByteArray());
Is there any workaround to make resulting image the same as source? different component, maybe?
Hold a backing Bitmap (originally the source) that you carry out your manipulations on.
Use the ImageView just to show this Bitmap - do not manipulate the post-scaled version that is held in the ImageView's drawing cache.