I want to pass image file to another broadcast receiver using intent, since the Android documentation suggest to pass data values not files.
How can I achieve this.
In order to pass image file to Android broadcast receiver you have to convert the file to bytes array and send using putExtra method
intent.putExtra("myImage", convertBitmapToByteArray(bitmapImage));
byte[] convertBitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
Log.e(BitmapUtils.class.getSimpleName(), "ByteArrayOutputStream was not closed");
}
}
}
}
Then you can convert back to image in your broadcast receiver
byte[] byteArray = intent.getByteArrayExtra("myImage");
Bitmap myImage = convertCompressedByteArrayToBitmap(byteArray);
Bitmap convertCompressedByteArrayToBitmap(byte[] src) {
return BitmapFactory.decodeByteArray(src, 0, src.length);
}
Related
I have ImageButton when click on it gallery will appear for pick an image and send bitmap back to show on this ImageButton.
But I have to get bitmap that has been shown on this ImageButton and then save it into database as byte[]
first get the bitmap from the ImgaeButton
Bitmap bitmap = ((BitmapDrawable)imageButton.getDrawable()).getBitmap();
then convert this bitmap to byteArray
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
byte[] byteArray = outputStream.toByteArray();
When you load image from gallery, you already have the URI reference to it, or you have the bitmap. Hope the following helps
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
Now, if you want to get bitmap from imageButton, you can use
Bitmap bitmap = ((BitmapDrawable)imageButton.getDrawable()).getBitmap();
Refer How to get Bitmap from an Uri? as well, to know more
Try
Bitmap bitmap = ((BitmapDrawable)imageButton.getDrawable()).getBitmap();
You can use a blob to store an image in sqlite android internal db.
*** below answer is completely copied from How to store image in SQLite database - credit goes to answer provider
public void insertImg(int id , Bitmap img ) {
byte[] data = getBitmapAsByteArray(img); // this is a function
insertStatement_logo.bindLong(1, id);
insertStatement_logo.bindBlob(2, data);
insertStatement_logo.executeInsert();
insertStatement_logo.clearBindings() ;
}
public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
to retrieve a image from db
public Bitmap getImage(int i){
String qu = "select img from table where feedid=" + i ;
Cursor cur = db.rawQuery(qu, null);
if (cur.moveToFirst()){
byte[] imgByte = cur.getBlob(0);
cur.close();
return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
}
if (cur != null && !cur.isClosed()) {
cur.close();
}
return null ;
}
When I share animate gif to Twitter by using Intent, Twitter makes it static image.
It works to share to Facebook Messenger.
How to create an animated GIF from JPEGs in Android (development)
I use this class to create Animated gif.
Can anyone help me?
Here is my code below.
public byte[] generateGIF(ArrayList<Bitmap> bitmaps) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(bos);
for (Bitmap bitmap : bitmaps) {
encoder.addFrame(bitmap);
}
encoder.finish();
return bos.toByteArray();
}
private void shareGif (byte[] bytes,String fileName) {
try {
File file = new File(getApplicationContext().getCacheDir(), fileName + ".gif");
FileOutputStream fOut = new FileOutputStream(file);
fOut.write(bytes);
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/gif");
startActivity(Intent.createChooser(intent , "Send image using.."));
} catch (Exception e) {
e.printStackTrace();
}
}
I want to load a lot of image like instagram do but still get OutOfMemory my code load 18 image at a time and can scroll down to load more.
I also cache image to disk and resize image to fit thumbnail before load to Bitmap
private Bitmap loadBitmap(String id) throws IOException{
String key = id.toLowerCase();
// Check disk cache in background thread
Bitmap image = cacher.get(key);
if (image == null){
// Not found in disk cache
// Process as normal
if(!isCancelled()){
//download image to stream
ByteArrayOutputStream stream = new ByteArrayOutputStream();
DriveApiActivity.getService().files().get(id)
.executeMediaAndDownloadTo(stream);
//decode image to byte array
byte[] byteArray = stream.toByteArray();
stream.close();
//decode byte array to bitmap file
image = decodeToBitmap(
byteArray,
CustomCardView.width,
CustomCardView.height);
// Add final bitmap to caches
cacher.put(key, image);
}
}
return image;
}
the Logcat say exception is come out of cacher.get() method
public Bitmap get(String key) {
synchronized (diskCacheLock) {
// Wait while disk cache is started from background thread
while (diskCacheStarting) {
try {
diskCacheLock.wait();
} catch (InterruptedException e) {
Toast.makeText(
context,
"getBitmapFromCache:" + e.getMessage(),
4).show();
}
}
if (diskLruCache != null) {
Bitmap bitmap = null;
DiskLruCache.Snapshot snapshot = null;
try{
snapshot = diskLruCache.get(key);
if(snapshot == null){
return null;
}
final InputStream in = snapshot.getInputStream(0);
if(in != null){
final BufferedInputStream buffIn =
new BufferedInputStream(in, Utils.IO_BUFFER_SIZE);
bitmap = BitmapFactory.decodeStream(buffIn);
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(snapshot != null){
snapshot.close();
}
}
if(BuildConfig.DEBUG){
Log.d( "cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key);
}
return bitmap;
}
}
return null;
}
this code come from DiskLruCache google provided
You have to use:
largeheap = true
in android. manifest file, from this line you cannot get out of memory error
It will solve your problem
I'm trying to create new MyImage entitly as listed in How to upload and store an image with google app engine.
Now I'm not using any Form. I have Android app that gets Uri from Gallery :
m_galleryIntent = new Intent();
m_galleryIntent.setType("image/*");
m_galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
m_profileButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
startActivityForResult(Intent.createChooser(m_galleryIntent, "Select Picture"),1);
}
});
And I'm using the Uri to create a Bitmap.
How can I create a Blob In my client from the Bitmap?
And what jars i'll have to add to my android project?
Is this a proper way to use Blob?
My main goal is to save an image uplodaed from an android in the GAE datastore, Am Using this tools properly or ther is better way?
Thatks.
You have to convert your Bitmap into a byte[]and after you can store it in your database as a Blob.
To convert a Bitmap into a byte[], you can use this :
Bitmap yourBitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();
I hope it's what you want.
you can use this code :
public static byte[] getBytesFromBitmap(Bitmap bitmap) {
if (bitmap!=null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
return null;
}
for converting bitmap to blob.
note:
blob is an binary large object.
I have a bitmap that I have saved in the external storage. I already have a method that loads and returns the bitmap. My question is, how do I attach this image to an email Intent.
Note: I know how to start the email intent, I simply need to know how to attach the bitmap. Thanks.
This is how I am saving the pic:
private void savePicture(String filename, Bitmap b, Context ctx) {
try {
FileOutputStream out;
out = ctx.openFileOutput(filename, Context.MODE_WORLD_READABLE);
b.compress(Bitmap.CompressFormat.JPEG, 40, out);
if (b.compress(Bitmap.CompressFormat.JPEG, 40, out) == true)
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
try this for Attach Image with Email
Fetch Image From SdCard
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path,"YourImageName.JPEG");
Uri pngUri = Uri.fromFile(file);
Email Intent
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);