I am very new to android development and trying to make an application that will convert a video to GIf using MediaMetadataRetriever and AnimationDrawable. I am able to display the GIF but I am not able to save the file. I want to know how I can save this GIF as a file or essentially an AnimationDrawable object to a file in JAVA.
Thanks
MediaMetadataRetriever mmRetriever = new MediaMetadataRetriever();
mmRetriever.setDataSource(MainActivity.this,result);
AnimationDrawable animatedGIF = new AnimationDrawable();
Bitmap bitmap = mmRetriever.getFrameAtTime(n); #n is any integer
Drawable d = (Drawable) new BitmapDrawable(getResources(), bitmap);
animatedGIF.addFrame(d,200);
binding.imageView.setImageDrawable(animatedGIF);
animatedGIF.setOneShot(false);
animatedGIF.start();
Related
I'm trying to share text from my app as an image to other apps. So I want a user to tap share on some content, have the app generate an image from the text, and add that to a chooser intent. (For example, sharing text from Twitter as an image on Instagram). I'm just not sure how to generate an image from the text and hand it in the proper format to the chooser. Any help is great, thanks!
One way is to create a Bitmap object from your TextView, storing it on disk and then sharing that file. This is how you can capture a View as a Bitmap object (courtesy of this answer):
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
The rest should be easy enough.
I have a image loader which loads the image and save it into the PC as follows:
ImageLoader saver = new ImageLoader();
saver.data = new ImageData[] { ImageDescriptor.createFromURL(
FileLocator.find(bundle, new Path("icons/img.gif"), null))
.createImage().getImageData() };
saver.save("D:/img.gif", SWT.IMAGE_GIF);
But when i trying to save animated gif, the saved image is not animated. How could i save the animated image from the bundle to the user PC ?
As the code show, you have only one ImageData.
For an animated GIF, you need several ImageData. ImageDescriptor doesn't allow that; it is too high level, you need to use SWT directly:
final ImageLoader loader = new ImageLoader();
loader.load(FileLocator.find(bundle, new Path("icons/img.gif"), null).openStream()); // closing the stream would be appreciable :)
You can then try to save directly, but I think the SWT library is unable to save animated GIF.If you still want to use SWT for that, you must save each image one by one:
int i=0;
for (ImageData data : loader.data) {
final ImageLoader saver = new ImageLoader();
saver.save("image-" + (i++) + ".gif", SWT.IMAGE_GIF);
}
I want to get Bitmap from ImageView. I have used following code, but getDrawable() returns null. How to get whole Bitmap from ImageView.
Bitmap bitmap;
if (mImageViewer.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageViewer.getDrawable()).getBitmap();
} else {
Drawable d = mImageViewer.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
storeImage(bitmap,"final.jpeg");
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)
Could be that the emulator or device your using has a different density and is trying to pull images from another folder.
If you are using an extension in your image other than .png, .jpg, or .gif, It might not recognize other extension types. http://developer.android.com/guide/topics/resources/drawable-resource.html
According to this answer, just do it like this:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
For Kotlin:
simply write this code to get the bitmap from an ImageView
imageview.invalidate()
val drawable = imageview.drawable
val bitmap = drawable.toBitmap()
If you are trying to get bitmap from Glide loaded image then this will help you
Drawable dr = ((ImageView) imView).getDrawable();
Bitmap bmp = ((GlideBitmapDrawable)dr.getCurrent()).getBitmap();
Take a picture of the ImagView and convert it to a string to send to the server
ImageView ivImage1 = (ImageView ) findViewById(R.id.img_add1_send );
getStringImage( ( ( BitmapDrawable ) ivImage1.getDrawable( ) ).getBitmap( ) ),
public String getStringImage(Bitmap bm){
ByteArrayOutputStream ba=new ByteArrayOutputStream( );
bm.compress( Bitmap.CompressFormat.PNG,90,ba );
byte[] by=ba.toByteArray();
String encod= Base64.encodeToString( by,Base64.DEFAULT );
return encod;
}
I am using Universal-Image-Loader and there is this functionality that access the file cache of the image from sd card. But I don't know how to convert the returned file cache into bitmap. Basically I just wanted to assign the bitmap to an ImageView.
File mSaveBit = imageLoader.getDiscCache().get(easyPuzzle);
Log.d("#ImageValue: ", ""+mSaveBit.toString());
mImageView.setImageBitmap(mSaveBit);
Error: "The method setImageBitmap(Bitmap) in the type ImageView is not applicable for the arguments (File)"
You should be able to use BitmapFactory:
File mSaveBit; // Your image file
String filePath = mSaveBit.getPath();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
Define File
String fileName = "/myImage.jpg";
File file = new File(fileName);
get Bitmap of Image
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
Set Bitmap to ImageView
myImageView.setImageBitmap(bitmap);
You can use this function to get Bitmap from file path
fun getBitmap(filePath:String):Bitmap?{
var bitmap:Bitmap?=null
try{
var f:File = File(path)
var options = BitmapFactory.Options()
options.inPreferredConfig = Bitmap.Config.ARGB_8888
bitmap = BitmapFactory.decodeStream(FileInputStream(f),null,options)
}catch (e:Exception){
}
return bitmap
}
Here is a simple code to create a scaled image for ImageView in this case
- Width:400
- Height:400
final File file = new File(Environment.getExternalStorageDirectory(),"b.jpg");
ImageView img = (ImageView) findViewById(R.id.imageview);
img.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()),400,400,false));
Kotlin Version
if (requestCode==PICK_IMAGE_REQUEST){
if (data!=null){
selectedfileUri=data.data
if (selectedfileUri!=null && !selectedfileUri!!.path.isEmpty()){
val file = FileUtils.getFile(context,selectedfileUri)
val bitmap = BitmapFactory.decodeFile(file.path)
uimg!!.setImageBitmap(bitmap)
}
}
}
This is not the right question, but if you use flag .cacheInMemory() in ImageLoader setup you can retrive the bitmap without need of recreate at any time using BitmapFactory to safe memory usage .
Just use:
Bitmap bitmap = ImageLoader.getInstance().getMemoryCache()·get("url as key");
In my Android App Activity, I have a RelativeLayout with one ImageView and a couple of TextViews being populated at runtime.
I also have a Save button in the activity that I use to save the image in the ImageView to the device SD Card.
Now what I really want to do is Convert the elements (image and the text in the RelativeLayout) together to a PNG image when the Save button is clicked and Save it to the SD Card.
Have anyone tried a conversion like this before? It would be very helpful if someone can give me some hints or code snippets on how to go about doing this?
The Save functionality works fine but currently only saves the image in the imageview.
Thanks in advance.
RelativeLayout is a subclass of View, and the following should work for any view:
final View v; // The view that you want to save as an image
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
v.draw(c);
File outputFile; // Where to save it
FileOutputStream out = new FileOutputStream(imageFile);
boolean success = bitmap.compress(CompressFormat.PNG, 100, out);
out.close();
Add exception handling at your leisure. ;)