Convert text and current background image of recyclerview to image and save it to device - kotlin

in my adapter i have add this code
holder.binding.saveButton.setOnClickListener {
mediaPlayer.start()
if (ContextCompat.checkSelfPermission(holder.itemView.context,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(holder.itemView.context as Activity,
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
REQUEST_WRITE_STORAGE)
} else {
val parentView = holder.itemView.findViewById<LinearLayout>(R.id.itemViewe)
parentView.isDrawingCacheEnabled = true
val bitmap = parentView.getDrawingCache()
// Create a file to save the image
val imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
val image = File.createTempFile("your_image_name", ".jpg", imagesDir)
// Compress the bitmap and save it to the file
val stream = FileOutputStream(image)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
stream.flush()
stream.close()
// Clear the drawing cache
parentView.destroyDrawingCache()
// Add the image to the system gallery
val values = ContentValues()
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis())
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
values.put(MediaStore.MediaColumns.DATA, image.absolutePath)
holder.itemView.context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
// Show a toast message to indicate that the image has been saved
Toast.makeText(holder.itemView.context, "Status Saved", Toast.LENGTH_SHORT).show()
}
}
when click on save button in recyclerview show permisiion to allow and after show toast status saved but in gallery its not showing ..... in my case my device android version 11 so image show in gallery but in version 12 when save button clicked show toast and show blank in gallery not full image.
it work on some version and not work in other versions but i want to work it for all device how can i do it

Related

How to share a recyclerview background image and text as one

I created a recyclerview for the purpose of sharing messages. Each item in the recyclerView has a different image(.png) background set as flower background with text set on it with TextView. It shows well. But whenever I clicked shareImage icon only image is shared.
Screenshot from recyclerview. This is how I want it to be
This is what I've got to share, the image and text are separated!
What I want to achieve?
I want to automatically crop and share but background image and text I set on it as a card.
This is what I did that didn't work
myHolder.mShareImage.setOnClickListener(v -> {
Drawable drawable= myHolder.mImageView.getDrawable();
Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();
try {
File file = new File(mContext.getApplicationContext().getExternalCacheDir(), File.separator +models.get(i).getTitle()+models.get(i).getIcon()+".png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent shareImagerIntent = new Intent(android.content.Intent.ACTION_SEND);
shareImagerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri photoURI = FileProvider.getUriForFile(mContext.getApplicationContext(), BuildConfig.APPLICATION_ID +".provider", file);
shareImagerIntent.putExtra(Intent.EXTRA_STREAM, photoURI);
shareImagerIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareImagerIntent.setType("*/*");
shareImagerIntent.putExtra(Intent.EXTRA_TEXT, models.get(i).getTitle());
mContext.startActivity(Intent.createChooser(shareImagerIntent, "Share image via"));
} catch (Exception e) {
e.printStackTrace();
}
});

How to create a camera view + a button to capture the photo in Xamarin.Forms?

I am trying to find a solution for an android app which I can have both camera preview and a button created with Xamarin Forms (XAML) that when I click that button photo should automatically save in device gallery. After 2 days of research the only best solution I found is this. Can someone please help me achieve this?
Refer to official sample , it implements creating camera preview , i just add the function of take picture and save into gallery .Call messaging center in forms project and get camera instance in android project ,
Call back of Picture Taken
public void OnPictureTaken(byte[] data, Camera camera)
{
camera.StopPreview();
FileOutputStream outStream = null;
Java.IO.File dataDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
if (data != null)
{
try
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
var s = ts.TotalMilliseconds;
outStream = new FileOutputStream(dataDir + "/" + s + ".jpg");
outStream.Write(data);
outStream.Close();
}
catch (Java.IO.FileNotFoundException e)
{
System.Console.Out.WriteLine(e.Message);
}
catch (Java.IO.IOException ie)
{
System.Console.Out.WriteLine(ie.Message);
}
}
camera.StartPreview();
}
Check my sample on github.

Screenshot of Mapbox map without displaying it

I need to display a few previews of addresses on the map on the same screen. To do that I'm going to display ImageViews with bitmaps instead of making multiple instances of MapView. But for that I need to find a way to capture these bitmaps.
I found snapshot function in MapboxMap (https://github.com/mapbox/mapbox-gl-native/issues/6062), but it requires displaying of the map.
val position = CameraPosition.Builder()
.target(addressLatLng)
.zoom(zoom)
.build()
mapboxMap.cameraPosition = position
mapboxMap.snapshot { bitmap: Bitmap ->
imageView.setImageBitmap(bitmap)
}
So, can I take a snapshot of the map without displaying it on the screen?
I've finally found the solution! The class I needed to use was MapSnapshotter.
val width = imageView.width
val height = imageView.height
val location = CameraPosition.Builder()
.target(LatLng(55.7558, 37.6173))
.zoom(16.0)
.build()
val options = MapSnapshotter.Options(width, height)
.withCameraPosition(location)
MapSnapshotter(context, options).start { snapshot ->
imageView.setImageBitmap(snapshot.bitmap)
}

Codename One Drag & Drop blackens dialog background

I'm developing an app that uses drag & drop operations. The drag & drop part includes four containers as drop targets and two draggable labels.
The app works as expected in the simulator, but when I install it on my Android phone, the background of the dialog (otherwise white) turns black for a swift moment as soon as the draggable components (labels) are released (then gets back to the original light color).
In the theme builder, I have set the background of the dialog to an almost white color (unselected, selected, pressed, disabled), but that doesn't show any effect.
Is there any turnaround to keep the background color stable? Any help would be appreciated.
Here is the relevant code excerpt:
Dialog dialog_W1 = new Dialog();
dialog_W1.setScrollableY(true);
dialog_W1.setLayout(new BoxLayout(2));
dialog_W1.setScrollableY(true);
dialog_W1.setTitle("Acerte o bichinho");
Image img_Cat = Image.createImage("/cat.png");
ScaleImageLabel label_Scaled_Image = new ScaleImageLabel(img_Cat);
label_Scaled_Image.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
label_Scaled_Image.setUIID("NewLabel");
Label label_1 = new Label("ga");
Label label_2 = new Label("to");
label_1.setUIID("LabelWord");
label_2.setUIID("LabelWord");
label_1.setDraggable(true);
label_2.setDraggable(true);
Container c_1 = new Container();
Container c_2 = new Container();
Container c_3 = new Container();
Container c_4 = new Container();
c_1.setDropTarget(true);
c_2.setDropTarget(true);
c_3.setDropTarget(true);
c_4.setDropTarget(true);
c_1.setLayout(new BoxLayout(2));
c_2.setLayout(new BoxLayout(2));
c_3.setLayout(new BoxLayout(2));
c_4.setLayout(new BoxLayout(2));
c_1.setUIID("Container_1");
c_2.setUIID("Container_1");
c_3.setUIID("Container_2");
c_4.setUIID("Container_2");
c_3.add(label_2);
c_4.add(label_1);
GridLayout gl = new GridLayout(2,2);
Container container_0 = new Container(gl);
container_0.add(c_1);
container_0.add(c_2);
container_0.add(c_3);
container_0.add(c_4);
ArrayList <Container> list_Containers = new ArrayList<>();
list_Containers.add(c_1);
list_Containers.add(c_2);
list_Containers.add(c_3);
list_Containers.add(c_4);
ArrayList <Label> list_Labels = new ArrayList<>();
list_Labels.add(label_1);
list_Labels.add(label_2);
for (Label label : list_Labels) {
label.addDragOverListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (Container c : list_Containers) {
int int_C = c.getComponentCount();
if (int_C == 1) {
c.setDropTarget(false);
} else if (int_C == 0) {
c.setDropTarget(true);
}
}
}
});
}
I suggest showing the Dialog as a modless dialog instead of using the show method use showModless() or similar.
Odd things like this can happen because of the EDT nesting inherent in modal dialogs, normally we don't recommend using dialog for anything more than simple notifications. When we use drag and drop from a dialog like component we use InteractionDialog.

Accessing saved image on a Windows Phone

I am creating a camera app for the Windows Phone but when I want to retrieve my saved photo as a BitmapImage, nothing seems to work. I am using IsolatedStorageSettings to save the path of the images as shown below:
Picture p = mediaLibrary.SavePictureToCameraRoll(fileName, e.ImageStream);
if (!settings.Contains("lastImageTaken"))
{
//GetPath() requires using Microsoft.Xna.Framework.Media.PhoneExtensions;
settings.Add("lastImageTaken", p.GetPath());
}
else
{
settings["lastImageTaken"] = p.GetPath();
}
settings.Save();
then once the app starts up, I try to retrieve that last photo taken as shown below:
lastImageTaken = IsolatedStorageSettings.ApplicationSettings["lastImageTaken"] as string;
Uri uri = new System.Uri(lastImageTaken, UriKind.RelativeOrAbsolute);
BitmapImage image = new BitmapImage(uri);
previouseImage.Source = image;
if (image.PixelWidth < 1)
debugText.Text += " FAILED";
I have also tried something like this:
Uri uri = new System.Uri("file:///" + lastImageTaken.Replace("\\", "/"), UriKind.RelativeOrAbsolute);
BitmapImage image = new BitmapImage(uri);
but nothing seems to display the image. The width of to image always is shown as 0 which displays a "FAILED" text on a debug text. lastImageTaken is shown as C:\Data\Users\Public\Camera Roll\SMCA_jpg.jpg
I have also added the capabilities of ID_CAP_MEDIALIB_PHOTO
It seems that you are saving the image to CameraRoll, but trying to retrieve image from IsolatedStorage. Those are two different storage areas, and they are accessed differently.
In order to save an image to IsolatedStorage you need to replace this :
library.SavePictureToCameraRoll(fileName, e.ImageStream);
with this :
using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
{
// Initialize the buffer for 4KB disk pages.
byte[] readBuffer = new byte[4096];
int bytesRead = -1;
// Copy the image to the local folder.
while ((bytesRead = e.ImageStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
targetStream.Write(readBuffer, 0, bytesRead);
}
}
}
Then access the image the same way you've described in your original post.
Source : http://msdn.microsoft.com/en-us/library/windows/apps/hh202956(v=vs.105).aspx#BKMK_SavingToTheMediaLibraryAndIsolatedStorage