简体   繁体   中英

Bitmap.CompressFormat file stream record 0 bytes

The method below is supposed to get the thumb Image from local video file in the android system.

The problem is that the created file is recorded with 0 bytes.

It appears that there is some problem with the recording of the file stream which I can not understand, I don't get any errors during the execution of the method just 0 bytes file.

When I try to retrieve the Length of the Memory stream, even though the length of that particular file is 1319, I still get the result of 0.

private void GenerateThumbImage()
{
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.SetDataSource(directory + "snake.mp4");
    Bitmap bitmapData = retriever.GetFrameAtTime(15);
    if (bitmapData != null)
    {
        FileStream fileStream = new FileStream(directory + "snake.jpeg", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 4096, true);
        MemoryStream stream = new MemoryStream();
        bitmapData.Compress(Bitmap.CompressFormat.Jpeg,85, stream);           
        stream.CopyTo(fileStream);
    }
}

You are declaring the stream before the bitMapData compress. This is an Android working sample

public ImageSource GenerateThumbImage(string url, long usecond)
{   
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.SetDataSource(url, new Dictionary<string, string>());
    Bitmap bitmap = retriever.GetFrameAtTime(usecond);
    if (bitmap != null)
    {
        MemoryStream stream = new MemoryStream();
        bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
        byte[] bitmapData = stream.ToArray();
        return ImageSource.FromStream(() => new MemoryStream(bitmapData));
    }
    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM