0

I'm trying to save a image from Bitmap and share it in whatsapp, but, when I save it in my phone and try to open it, the image goes all black, so, when I share it, I share a black image. In other phone that I tested, I could open the saved image, but in this phone that I'm testing, I can't. Both are the same Android version. In this phone that appears a black image, I can see on the preview (gallery) the photo, but, it goes black when I try to open it.

This is my code to save the bitmap:

 public SKBitmap SaveImage(float ySize, SKBitmap bmpFormatted, SKBitmap standardBmp, int SKCanvasHeight)
        {
            float width = (float)screenSize.Width;
            bmpFormatted = new SKBitmap((int)width, SKCanvasHeight);
            SKRectI rect = new SKRectI(0, 0, (int)width, (int)ySize);
            standardBmp.ExtractSubset(bmpFormatted, rect);

            SKData data = bmpFormatted.Encode(SKEncodedImageFormat.Png, 100);
            byte[] bmpByte = data.ToArray();
            string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
            string path = Path.Combine(directory, "bmpSaved.png");

            File.WriteAllBytes(path, bmpByte);

            return bmpFormatted;
        }

And this is my code to share the saved bitmap:

string directory = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
            string path = Path.Combine(directory, "bmpSaved.png");

            var _context = Android.App.Application.Context;

            Intent sendIntent = new Intent(global::Android.Content.Intent.ActionSend);

            sendIntent.PutExtra(global::Android.Content.Intent.ExtraText, "Whatsapp");
            sendIntent.SetType("image/*");

            sendIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("file://" + path));
            _context.StartActivity(Intent.CreateChooser(sendIntent, "Share"));
            return Task.FromResult(0);

Is there any kind of difference or another way to do it? Because I need to it works in both phones. Any ideas?

techie
  • 463
  • 4
  • 17
  • Attach the not-working phone to computer, Start Debugging - so can see log messages in Visual Studio's Output pane. Any warnings or errors at the time you save the image? Put a breakpoint at line after `byte[] bmpByte = ...`. There should be some non-zero bytes at the start (header information), but at a certain point, does that data all become 0's (black)? Is the length of `bmpByte` the same on both phones? Given that it works on one phone, you need to debug what is happening on the two phones, find out EXACTLY which line of code has a different value. – ToolmakerSteve Aug 09 '21 at 18:31
  • *"I can see on the preview (gallery) the photo, but, it goes black when I try to open it."* - What are the values for `width` and `ySize` on both phones? After doing `File.WriteAllBytes(path, bmpByte)`, use `FileInfo.Length(path)` - is that value at least as big as `bmpByte.Length`? – ToolmakerSteve Aug 09 '21 at 18:43
  • 2
    what is the origin of the image you are trying to save? And have you checked the "bad" image with some sort of external image editor to see how it differs from a "good" image? – Jason Aug 09 '21 at 18:47
  • @Jason and @ToolmakerSteve, I tried to open this saved image on my computer to see if it is corrupted, but it didn't appear to open. Is it some permission? I have `WRITE_EXTERNAL_STORAGE` and `READ_EXTERNAL_STORAGE` checked. I tried to save it in DirectoryDownloads as my code is showing and `GetExternalStoragePublicDirectory("Temp/");`. None of that opened the image. ToolmakerSteve, I will see now the `bmpByte.Length`. – techie Aug 09 '21 at 19:21
  • @ToolmakerSteve same length. – techie Aug 09 '21 at 19:26
  • "it didn't appear to open" - what does this mean? What tool are you using to open it? Did you actually copy it from your device to the desktop? – Jason Aug 09 '21 at 19:35
  • @Jason I can't, because the file doesn't appear in my computer. All of the other files appear, but this saved image doesn't appear. It only appears in my phone and I tried to use Google Photos and QuickPic to open in my phone, with black image as result. – techie Aug 09 '21 at 19:39
  • 1
    use adb to copy it to your desktop – Jason Aug 09 '21 at 19:40
  • EDIT: I tried to save it in `Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal));` and when goes to open Whatsapp it shows: "the file format is not supported". – techie Aug 09 '21 at 19:42
  • @Que44 - Good - the Whatsapp message confirms that something is wrong with what is written to the file. Hopefully if you run under the debugger, there will be some warning or error in Visual Studio's output pane, at the time the file is written. I'm quite surprised that it is the same length. What is the length? – ToolmakerSteve Aug 09 '21 at 20:06
  • Gentlemens, I think it is permission... I saw this problem: https://stackoverflow.com/questions/48172519/oreo-write-external-storage-permission and I think it's reading, but when it will write, it just create, I think, the raw image, but with no byte processed in it, that's why it doesn't appear, it's corrupted. I'm having the same problem with the permission, is denied even I put to prompt it (it doesn't prompt, just to read). Did you know something to force it? I tried this `tools:node="replace"` but doesn't prompt. Doesn't make a lot of sense because both of phones are the same version – techie Aug 09 '21 at 20:14
  • If it was permission, then there would be no file at all. Not even the path. How did you tell WhatsApp what the file path/name was? – ToolmakerSteve Aug 09 '21 at 20:16
  • Actually, `SpecialFolder.Personal` is specific to the APP I think. Another app won't see it at all. – ToolmakerSteve Aug 09 '21 at 20:17
  • @ToolmakerSteve Yes, I know, I'm thinking about it, but my permission gets denied even if I put to prompt it. I really don't know what to do. It's too weird in just one phone works. – techie Aug 09 '21 at 20:19
  • For any new answer, please, let's go to chat to avoid extended discussions here. – techie Aug 09 '21 at 20:21
  • @Jason, do you know how to make a three-way chat? – ToolmakerSteve Aug 09 '21 at 20:22
  • no, I don't chat. I have a job. You and I have both asked multiple questions and made multiple suggestions that the OP appears to be ignoring in favor of trying random things. – Jason Aug 09 '21 at 20:33
  • Actually, I'm signing off for the day. If its a problem with writing the file, then you need to find out 1) does the file get created? 2) does the file have the length it should have, to hold all of bmpByte? One approach is what Jason said: use adb to copy file to computer. Once there, it is easy to examine as bytes (not as an image). Download any utility that can display the bytes of a file. Second approach is, after FIle.WriteAllBytes, try to read that file back into memory. Set a breakpoint, look at what you read back (as a byte array). Is it same contents as bmpBytes? – ToolmakerSteve Aug 09 '21 at 20:33
  • 1
    Ok gentlemens, I found the answer. I don't know why, but this type of Save Image that I did doesn't work on some type of phones (??), so I did another one from this link and it worked in both phones: https://stackoverflow.com/a/59393550/14700237 . I will do an answer to it if someone has the same problem that I was having. Thanks for the answers and support. – techie Aug 09 '21 at 22:44

1 Answers1

1

So if someone has the same problem that I was having, try this (for me it worked):

using (var image = surface.Snapshot())
            using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
            using (var stream = File.OpenWrite(Path.Combine(directory, "1.png")))
            {
                // save the data to a stream
                data.SaveTo(stream);
            }

Thanks to: https://stackoverflow.com/a/59393550/14700237

techie
  • 463
  • 4
  • 17