For the past few days, I am trying to set the icon of one of my Buttons as a GIF file programmatically. My GIF is located in my assets.
This is the code that I tried. It partially works, but the GIF seems to be stopped and is not playing, like just an ordinary PNG file.
AssetManager assetManager = context.getAssets();
Drawable drawable = null;
try {
drawable = Drawable.createFromStream(context.getAssets().open("test.gif"), null);
button.setCompoundDrawablesWithIntrinsicBounds(drawable, 0, 0, 0)
} catch (IOException e) {
e.printStackTrace();
}
I am expecting the drawable to be playing, just like a GIF.
Any help is appreciated. Thank you.
AssetManager assetManager = context.getAssets();
GifDrawable drawable = null;
try {
drawable = new GifDrawable(context.getAssets(), path);
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
} catch (IOException e) {
e.printStackTrace();
}
Hey, hope this helps you with your question, you could add this to your .gradle file.
Related
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.
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'
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);
I want to load an image from its URL in Fragment class. I'm loading the image like this:
try {
// ImageView j=(ImageView)rootView.findViewById(R.id.image_frm);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(image_url).getContent());
Bitmap resized = Bitmap.createScaledBitmap(bitmap, 600, 600, true);
Bitmap conv_bm = getRoundedRectBitmap(resized, 255);
i.setImageBitmap(conv_bm);
// j.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
but this method is taking more time to load the image and to open it.
If I want to use ImageLoader class like this tutorial, then how to load it in Fragment?
Use any 3rd party library like Picasso. Download the .jar file and put the it inside your libs folder. For more info see Picasso's documentation.
in case of Fragments, use getActivity() instead of context.
For rounded ImageView, I use the RoundedImageView lib.
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);