Move image from gallery to aplication protected folder in java android - java

When the user selects an image from gallery, it becomes visible in an ImageView and its directory is saved. If the image is deleted the directory will be null, crashing the app. I want to take the image and move it to a secrect/protected folder only visible by the application. Is it possible? How can I do that?

There's Base64 Android class:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
convert your image into a byte array though. Here's an example:
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
Have a look here : http://androidcodemonkey.blogspot.com/2010/03/how-to-base64-encode-decode-android.html
or just google on how to use Base64 and then store the string in your private/protected application area.

Related

Convert .gif image to base64 to upload to server

I'm trying to encode an animated gif to base64, so I can upload it to server. I am already doing it with static images, but if I try to do it with GIF, it simply does not work. I get the image from Intent image picker and put it inside an ImageView.
Here is the code I use to encode images to base64:
Bitmap bitmap = ((BitmapDrawable) ((ImageView) nextChild).getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] imginbyte = baos.toByteArray();
value = Base64.encodeToString(imginbyte, Base64.DEFAULT);
try this :
byte[] imginbyte = Base64.decodeBase64(bitmap.getImageBase64());
new FileOutputStream("image2.gif");
write(imginbyte);
close();

Get Image From Image view and send This image to other fragment with encode bitmap

i want get image From server and put to this image view i want get image from this imageview and compress with bitmap and send this picture to other fragment i dont want add 2 request for my server
my code is :
Bitmap bmp = BitmapFactory.decodeResource(getResources(),"my problem");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
mBundle.putByteArray("ResID", byteArray);
i know if i put R.darwable.ic_luncher it will work
but i want take this image from image view and put to " my probem "
can u help me ?
You can use mImageView.getDrawable() to get Drawable object from ImageView:
Drawable drawable = mImageView.getDrawable();
After that you can obtain a Bitmap object from it:
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
Bitmap bitmap = bitmapDrawable.getBitmap();
and further, if you want, even InputStream for byte array to send it:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Here, the CompressFormat and quality can be adjusted for output image size
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageBytes = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
As a result, imageBytes will contain all the bytes of the image, which you can send over elsewhere (in your scenario: to your other Fragment)

"SkImageDecoder::Factory returned null " when trying to decode byte array into imageView - Android

In my app I try to store an image from an imageView as a byte array and store that in a databse
ImageView imageView = (ImageView) findViewById(R.id.imgpreview);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArray = baos.toByteArray();
This byteArray is then stored in a database, and extracted back out inside a custom GridAdapter so that the byte array can be converted back to a bitmap and set as the image view.
ImageView image = (ImageView)gridView.findViewById(R.id.image1);
TextView description = (TextView)gridView.findViewById(R.id.tv1);
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray.get(position), 0, byteArray.get(position).length);
image.setImageBitmap(bmp);
It doesn't seem to be a problem with storing or retrieving the byte array, as I log a toString of the byte array just before it attempts to decode it and it appears to be correctly logged, so I'm confused as to why it's being shown as null, the exact message is:
D/skia: --- SkImageDecoder::Factory returned null
I've looked through all the threads with solutions I could find and tried them all, I just can't figure out how to fix it. I've tried with varying image sizes. If any more info is needed let me know, thanks in advance.

Why can't I decode Android encoding image using base64String with an other decoder?

I have an image that I encode using this code :
Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
It encodes well. After that I'm getting the String in a file in my computer and I try to decode it with this website :
http://www.askapache.com/online-tools/base64-image-converter/
And I'm getting a null picture. However, when I decode it directly in my app and display it like :
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
private ImageView img;
img.setImageBitmap(decodeBase64(encodedImage));
I'm getting the right picture. So I was wondering if i actually can't decode an android encoded image with an other decoder than android's one or my file is corrupted meaning that I have missing characters. However even if I don't have the whole encoded file, the decoded picture i'm getting with the website should look like to the good one right ?
Thank you in advance

Reading an image as byte array with Android: skImageDecoder::Factory returned null

I have a Spring MVC Web Service that returns an image as a byte array. The output is in json format. The format is png
Here's the code snippet for the image.
BufferedImage img = ImageIO.read(new File(caminho.replace("/", "//")));
imagem = ((DataBufferByte) img.getRaster().getDataBuffer
()).getData();
When I run the server, this is the output:
[{"id":0,"caminhoMDPI":null,"caminhoHDPI":"C:/Users/Marcos/Pictures/postos/drawable-
mdpi/esso_logo.png","caminhoXHDPI":null,"caminhoXXHDPI":null,"imagem":"AAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAA....]
Eventually, other symbols appear on the "imagem" field. I suppose this is right, but I'm not
sure.
On my Android app, I have a routine to download the image and store it as a blob in the database. I receive the json format and transform it into a class with jackson. I've logged the "imagem" field and it looks the same.
My problem is that I can't transform it into an image. Here's the code snippet:
byte[] img_bandeira = cursor.getBlob(cursor.getColumnIndex("img_bandeira"));
Bitmap bmpBandeira = BitmapFactory.decodeByteArray(img_bandeira, 0, img_bandeira.length);
ImageView ivBandeira = (ImageView) view.findViewById(R.id.ivBandeira);
ivBandeira.setImageBitmap(bmpBandeira);
All I get is a message: skImageDecoder::Factory returned null.
I've looked at other similar posts, tried to change some lines, but nothing happened. I don't know what's going on here. Thanks in advance.
I solved it. In the server, the image should be sent like this:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage img = ImageIO.read(new File(caminho.replace("/", "//")));
ImageIO.write(img, "png", baos);
baos.flush();
String base64String = Base64.encode(baos.toByteArray());
baos.close();
In my app, it should be read like this:
public static Bitmap getImage(byte[] imageArray) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageArray);
Bitmap bitmap = BitmapFactory.decodeStream(byteArrayInputStream);
return bitmap;
}
Thank you, everybody ;)

Categories

Resources