I am trying to build a function for the android phone that will open the CameraAPI and then send the captured picture to a new activity.
In this activity i need it to show a preview for the image and then save.
How could i go about doing this without having to save the image to the SD card?
You can store the image into a bitmap and then convert it to byte array and then transfer to other activity through this code below
Bitmap bitmapPicture= "PICTURE FROM CAMERA";
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
Then Pass Through Intent
Intent imagepass = new Intent(Cam.this,Preview.class);
imagepass.putExtra("imagepass", bytes.toByteArray());
startActivity(imagepass);
In Other Activity we have to recieve the byte array and convert to bitmap and display in imageview through
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("imagepass");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView iv=(ImageView) findViewById(R.id.imgvw2);
iv.setImageBitmap(bmp);
You will have to use custom camera control Hope This Helps.
Related
How to compress jpg/bmp files which I can store in the memory then when needed decompress those images and show to users without losing too much of image quality? How to do the compress and decompress, any guidance/ link would be helpful.
Thank you
create image thumbnails
'byte[] imageData = null;
try
{
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(fileName);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
}
catch(Exception ex) {
}`
if u need for thumbNail then use
ThumbNailUtils.extractThumbnail(Bitmap source,int width,int height)
it show thumbnail from bitmap and when u want to show original bitmap then show bitmap
.
use wisely bitmap object cause it take more memory at runtime.
I had a problem where some of my images are loaded inside imageview rotated. I solved the problem by loading an image using Glide.
The image is loaded inside 100 x 100 imageview. Can I retrieve the image which is stored inside that imageview as 100 x 100 and upload it to the server instead of sending the original image?
Yes you can
public byte[] getImageFromImageView(ImageView imageView,String filePath) {
Drawable imageDrawable = imageView.getDrawable();
Bitmap imageBitmap = ((BitmapDrawable)imageDrawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
Then upload file to server or you can create RequestBodyand send it to server if you using Retrofit.
I have an app that takes a picture, assigns it to an imageview and then uploads it to a parse server.
The problem is that I need the images to be 200 x 200 px before upload.
Is there a way to do this programatically?
this is my function so far:
public void Myfunction (){
//image
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);
byte[] imageInByte = baos.toByteArray();
ParseFile file = new ParseFile("myimage.jpg",imageInByte);
Could anyone help me please?
To scale bitmap down before compress, add
bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false));
If your bitmap is not square, you probably want to crop it before scaling
I have the following code for this purpose in my app but it does nothing, and halts the application:
bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0, byteImage2.length));
Try like this
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
Then I suppose you draw the image too like this:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bmImage, 0, 0, paint);
use below line to convert bytes into Bitmap, it is working for me.
Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
you need to put above line outside of loop, as it takes Bytes Array and convert into Bitmap.
P.S. :- here imageData is bytes array of Image
Try this :
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 95, blob);
byte[] bitmapdata = blob.toByteArray();
Bitmap bmp = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
ImageView image = (ImageView) findViewById(R.id.ivPhoto);
image.setImageBitmap(bmp);
I have an app that takes a screenshot and the shares this with the share intent. Rather than save multiple images each time a user wants to simply share the image. Below is the code I used for images saved to the SD
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
dir = "file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Folder/" + location;
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(dir));
startActivity(Intent.createChooser(share, "Share Image"));
However I cant figure out how to simply save a Bitmap such as...
share.putExtra(Intent.EXTRA_STREAM, theBitmap);
Is there a way to do this without saving the image?
Thanks
What I eneded up doing was saving one "Temp" image to the SD/Phone and just overwrite the each time.
Try using this one:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
Uri uri = Uri.parse("android.resource://com.your.app/drawable/" + yourimage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareImage);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Send your image"));
This code works fine for me to send drawables that I use in my app. No pictures saved on sd card. The only problem is that it doesn't work with some social apps :/
Yes you can send the image without having to save it as a file. From just looking at the code you posted I have no idea how your sending the image but you convert the file with the following:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] data;
if(yourBitmap!=null){
yourBitmap.compress(CompressFormat.PNG, 75, bos);
}else if (yourBitmap==null){
Log.w("Image going to server is null","Lost in memory");
}
try{
data = bos.toByteArray();
I dont know if your sending the image to another user via your app or what but thats how I use it upload to a server. When your done with the image you would just nullify it all like this.
bos = null;
data = null;
yourBitmap = null;