Downloaded Bitmap loses transparency when added to an ImageView - java

I am trying to show a potentially transparent image from a remote location, but the alpha channel seems to be colored white when I add it to my ImageView.
I am downloading a remote image with the following code:
public static Bitmap loadBitmap(String url) throws IOException {
int bufferSize = 1024;
InputStream in = new BufferedInputStream(new URL(url).openStream(), bufferSize);
Bitmap bitmap = BitmapFactory.decodeStream(in);
try {
in.close();
} catch (Exception ignored) {
}
return bitmap;
}
In my Activity with the mentioned ImageView I have the following code (try-catch omitted):
if (bitmap != null) {
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
imageView.setImageBitmap(bitmap);
}
bitmap.getConfig() returns ARGB_8888.
I am coding against Android 1.6, i.e. SDK version 4.
Am I missing some magic setter? When I load the exact same picture as a Drawable from my res folder it works fine.
I noticed a setter called setHasAlpha on Bitmap, but this is since SDK level 12.
EDIT:
I tried getting the color of some of the pixels I know are transparent, and their color == 0, which is transparent.

Sounds like the ImageView itself has a background. Try setting the background color to transparent (eg, imageView.setBackgroundColor(0);

use compress format as PNG
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);
bmpimg.compress(CompressFormat.PNG, 20, bos);

Related

Screenshot not captured while agora.io video call Android Java

I have implemented a video calling feature in our app. There is a button which captures the screenshot of specific view. On video call there a two views Local View and Remote View. These are actually Relative and Frame layouts. In this case i want take screenshot of remote view but it's just giving me a blank image. I think somehow agora video calling not allowing it but there should be any settings or something to disable this but that i have not found yet. here is the code:
Setting up remote video:
mRemoteContainer is a RelativeLayout and mRemoteVideo is VideoCanvas by agora.io
Similarly mLocalContainer is FrameLayout.
private void setupRemoteVideo(int uid) {
ViewGroup parent = mRemoteContainer;
if (parent.indexOfChild(mLocalVideo.view) > -1) {
parent = mLocalContainer;
}
if (mRemoteVideo != null) {
return;
}
SurfaceView view = RtcEngine.CreateRendererView(getBaseContext());
view.setZOrderMediaOverlay(parent == mLocalContainer);
parent.addView(view);
mRemoteVideo = new VideoCanvas(view, VideoCanvas.RENDER_MODE_HIDDEN, uid);
mRemoteVideo.view.setWillNotDraw(false);
mRtcEngine.setupRemoteVideo(mRemoteVideo);
}
This function used to get bitmap from the view
private Bitmap getScreenShotFromView(View v) {
Bitmap screeShoot = null;
try {
screeShoot = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(screeShoot);
v.draw(canvas);
} catch (Exception e) {
Log.e("GFG", "Failed to capture screenshot because:" + e.getMessage());
}
return screeShoot;
}
I tried function call with both view but getting black or blank image
Bitmap bitmap = getScreenShotFromView(mRemoteVideo.view);
Bitmap bitmap = getScreenShotFromView(mRemoteContainer);
Your help will be really appreciated for me as i stuck on this issue.
By the way I used the same view capturing technique on iOS with swift
and Its working fine.

How to save a bitmap after applying effects

I have some buttons and one image view. I load a photo from gallery,the photo is displayed on image view. For every button,there is on click method to apply that applies one of the effects for the image. There is another button,that is for saving the photo on internal storage. I want to save the photo after one of the effects is applied. I searched,but I just find how to save the photo without effect . Does somebody know how with the on click method to save the image AFTER the effect is applied .
you can simply get the bitmap from ImageView by calling this function:
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap()
and then save your bitmap where you want with in/out streams:
private void saveBitmapFromImageView(File destFile, ImageView imageView){
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap()
OutputStream os;
try {
os = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
} catch(Exception e) {
Log.e(TAG, "saveBitmapFromImageView", e);
} finally {
IOUtils.closeQuietly(os);
}
}
Edit #1:
IOUtils is Apache commons lib, add to your gradle:
compile 'org.apache.commons:commons-io:1.3.2'

Trying to load and read images from SD card android

What I am trying to do:
I am trying to load all the images that I have onto the phones SD card when the app is created. I then have a button and when I click the button, the next image should show.
What I've done:
So far I have included this code in the onCreate() method, however I don't know how to add multiple images into the folder.
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "uct.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
At the moment I have an array of of image IDs, these images are stored in the Drawable folder at the moment.
int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
Then I have an onClick() method that increments a private index variable to set the Image Resource like so (here imgView is the ImageView).
imgView.setImageResource(images[current_image_index]);
What I don't know how to do:
Firstly, I don't know how to add multiple images to the SD card in the onCreate() method. Also I am not sure what the best way to go about loading the 'correct' image from the SD when the button is clicked.
I hope this makes sense, please remark if anything needs to be clarified.
If i were in your case,
Create the ViewPager to show all images.
And you can get all images url using this code.
String[] STAR = {"*"};
ArrayList<String> imageUri = new ArrayList<>();
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI
, STAR, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String path = cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DATA));
Log.i("Path", path);
imageUri.add(path);
getDate(path);
} while (cursor.moveToNext());
}
}
You can pass the imageUri ArrayList to Viewpager adapter.At viewPager
adpter you can use any image library to load image. In case of Picasso.
Picasso.with(context)
.load(uri)
.resize(150,150)
.centerCrop()
.into(imageView);

How to show a captured image in an ImageView?

I'm trying to display a captured image from a camera intent, but when I call a showPhoto method the image isn't shown in the ImageView. There is no error output to the stack trace so I'm not sure how the issue can be debugged.
Does anyone know what is wrong with the method that its not showing the captured image?
The showPhoto method is as follows:
private void showPhoto(Uri photoUri) {
String filePath = photoUri.getEncodedPath();
File imageFile = new File(filePath);
//File imageFile = new File(photoUri.getPath());
if (imageFile.exists()){
Drawable oldDrawable = photoImage.getDrawable();
if (oldDrawable != null) {
((BitmapDrawable)oldDrawable).getBitmap().recycle();
}
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
photoImage.setImageDrawable(drawable);
}
}
This is the tutorial that I've followed: http://www.linux.com/learn/tutorials/722038-android-calling-the-camera
The device I'm testing on is JellyBean 4.1 and this is the link to the complete class:
http://hastebin.com/oqokupulol.java
Use BitmapOptions.inJustDecodeBounds to find out the dimension of the image first
Typically, Android won't load images larger than 4000px wide/tall
If you find out the image is too large, you can use BitmapOptions.inSampleSize to downscale the image so that Android would load it
And in fact, you can directly use ImageView.setImageBitmap(Bitmap) instead of setImageDrawable

Getting image data continuously from camera, SurfaceView or SurfaceHolder

So I have this camera preview set up with Camera, SurfaceView and SurfaceHolder.
I have also an ImageView where I will be putting a modified version of the camera image and I want this to update lets say once every second.
All code is ready and already working when I load images from "res" but I have a really hard time reading the image data from the camera.
I've tried following already:
Creating an intent for MediaStore.ACTION_IMAGE_CAPTURE and starting an onActivityResult getting a small thumbnail (enough for me actually) from (Bitmap)data.getExtras().get("data")
The problem is that this opens the camera App and you need to "manually" take a picture.
Creating a Camera.PreviewCallback, taking the YuvImage, and converting it to an image using YuvImage.compressToJpeg(...).
The problem here is that I can't get it to start no matter when or where i put the Camera.setPreviewCallbackWithBuffer(PreviewCallback).
Try to take the data directly from PreviewHolder by locking in to the canvas using lockCanvas() and trying to convert it to a bitmap
Obviously Doesn't work.
Edit:
What is the best way to make this work? I mean QR-Code readers must read the image data out of the camera continuously, how do they work?
I went for option number 2 and finally made it work.
used this callback, forgot the #Override before
private Camera.PreviewCallback previewCallback= new Camera.PreviewCallback()
{
#Override
public void onPreviewFrame(byte[] data,Camera cam)
{
Camera.Size previewSize = cam.getParameters().getPreviewSize();
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21,previewSize.width,previewSize.height, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0,0,previewSize.width,previewSize.height),80,baos);
byte[] jdata = baos.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jdata,0,jdata.length);
}
};
And initiating it using setPreviewCallback rather than setPreviewCallbackWithBuffer
SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback()
{
public void surfaceCreated(SurfaceHolder holder) {
camera.setPreviewCallback(previewCallback);
}
}

Categories