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.
Related
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();
I'm trying to get the text input from the user and draw it on the image using Canvas but the image is saved without what was supposed to be drawn. Right now, I'm just trying to get the text on the Image before I worry about the font, colour, styles, etc.
This is my code:
public void createBitmapAndSave(ImageView img){
BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
Bitmap bitmap = bitmapDrawable.getBitmap();
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(200);
paint.setStyle(Paint.Style.FILL);
paint.setShadowLayer(10f, 10f, 10f, Color.BLACK);
String topText = topTextView.getText().toString();
String bottomText = bottomTextView.getText().toString();
canvas.drawText(topText, 0, 0, paint);
canvas.drawText(bottomText, 50, 50, paint);
File file;
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();
file = new File(path + "/SimpliMeme/" + timeStamp + "-" + counter + ".jpg");
file.getParentFile().mkdir();
try{
OutputStream stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
stream.flush();
stream.close();
Toast.makeText(getContext(), "Meme Saved", Toast.LENGTH_SHORT).show();
}
catch (IOException e){ e.printStackTrace();}
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
Objects.requireNonNull(getContext()).sendBroadcast(mediaScanIntent);
counter++;
}
At the moment, I only have the 2 .drawText() implementations based on the examples that I've seen in other SO posts. My assumption is that the text isn't visible and no changes are made to the image because I haven't provided the paint object with any attributes.
The main issue why you see no changes is that you make changes to mutableBitmap but save the original bitmap to disk.
This can be avoided by joining the first two (or even three) statements together:
final Bitmap bitmap = bitmapDrawable.getBitmap()
.copy(Bitmap.Config.ARGB_8888, true);
You didn't need the orginal bitmap anywhere else, this effectively prevents you from making the mistake. Don't do what you don't need to do.
Some tips:
Always be explicit when drawing. Specify the color, specify the font. You can't trust default values. (At least I'm never sure about the values. Is the default color black or transparent?)
If you want to be asolutely sure about the font, bundle it with your app or use downloadable fonts. Some platforms allow the user to change the default font to something crazy.
If you ever want to draw multiline text look into StaticLayout.
Make sure your app works on Android 7 and above. Sending intents with file Uri outside your app is prohibited.
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);
}
This is my code thus far
ImageLoader saver = new ImageLoader();
saver.data = new ImageData[]
{ toSave.getImageData() };
saver.save(fileName, SWT.IMAGE_PNG);
toSave is an image that was loaded using the SWTResourceManager that is transparent in the program. fileName is a string representing the file I want to save the image to (ends with .png)
The result is an image with a black background instead of a transparent background that I want. How do I make the background transparent? I think it has something to do with the transparency mask, but I could be wrong.
Thanks in advance!
SWTResourceManager seems to be causing your problem. I would not recommend to use it.
Try this code, it works for me:
Display d = Display.getDefault();
Image image = new Image(d, "/pictures/Llama.gif");
ImageLoader saver = new ImageLoader();
saver.data = new ImageData[] { image.getImageData() };
saver.save("output.png", SWT.IMAGE_PNG);
image.dispose();
Remember to dispose the Image when you don't need it anymore.
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. ;)