Android的保存GIF从数据库到文件 [英] Android save gif from database to file

查看:349
本文介绍了Android的保存GIF从数据库到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序的数据库,我已经保存GIF图像的BLOB。我怎样才能从retrive这个数据库的gif,并将其保存到SD卡作为gif文件?对PNG和JPEG我知道如何做到这一点。如何我没有任何库正确实现这一目标?
对PNG和JPEG我做这样的code:

  ByteArrayInputStream进行的ImageStream;
的ImageStream =新ByteArrayInputStream进行(imageBlob);
位图图像= BitmapFactory.de codeStream(的ImageStream);
storeImage(图片);
私人无效storeImage(位图图像​​){
    文件PictureFile的= getOutputMediaFile();
    如果(PictureFile的== NULL)
    {
        返回;
    }
    尝试{
        FOS的FileOutputStream =新的FileOutputStream(PictureFile的);
        image.com preSS(Bitmap.Com pressFormat.PNG,90,FOS);
        fos.close();
    }赶上(FileNotFoundException异常五){
        Log.i(MyApp的,未找到文件:+ e.getMessage());
    }赶上(IOException异常五){
        Log.i(MyApp的,访问文件时出错:+ e.getMessage());
    }
}//为了获取图像存储路径
私人文件getOutputMediaFile(){    文件mediaStorageDir =新的文件(Environment.getExternalStorageDirectory()
            +/ Android的/数据/
            + contexto.getPackageName()
            +/文件);
    //创建存储目录,如果它不存在
    如果(!mediaStorageDir.exists()){
        如果(!mediaStorageDir.mkdirs()){
            返回null;
        }
    }    //创建一个媒体文件名
    文件媒体文件;    字符串mImageName =myImage.png;
    媒体文件=新的文件(mediaStorageDir.getPath()+文件分割符+ mImageName);    返回媒体文件;
}

我没有成功尝试以下。这并不创建该文件。我不知道什么是错的,或者如果即时缺少的东西。

  ByteArrayInputStream进行的ImageStream =新ByteArrayInputStream进行(imageBlob);
ByteArrayOutputStream的ByteBuffer =新ByteArrayOutputStream();
INT缓冲区大小= 1024;
字节[]缓冲区=新的字节[缓冲区大小]
INT LEN = 0;
而((LEN = imageStream.read(缓冲液))!= - 1){
      byteBuffer.write(缓冲液,0,LEN);
}
的FileOutputStream流=新的FileOutputStream(mypath中);
stream.write(byteBuffer.toByteArray());

需要帮助,请。先谢谢了。


解决方案

  ByteArrayInputStream进行的ImageStream =新ByteArrayInputStream进行(imageBlob);

一旦你有了,只是保存到文件流。不要做它的一个位图,不使用BitmapFactory。您可以使用为PNG,JPG和GIF。

In the database of my app i have stored a gif image as BLOB. How can i retrive this gif from database and save it to sdcard as gif file? For png and jpeg i know how to do it. How can i achieve this correctly without any library? For png and jpeg i do like this code:

ByteArrayInputStream imageStream;
imageStream = new ByteArrayInputStream(imageBlob);
Bitmap image= BitmapFactory.decodeStream(imageStream);
storeImage(image);


private void storeImage(Bitmap image) {
    File pictureFile = getOutputMediaFile();
    if (pictureFile == null) 
    {
        return;
    }
    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        image.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.i("MyApp", "File not found: " + e.getMessage());
    } catch (IOException e) {
        Log.i("MyApp", "Error accessing file: " + e.getMessage());
    }
}

//To Get the Path for Image Storage
private  File getOutputMediaFile(){

    File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
            + "/Android/data/"
            + contexto.getPackageName()
            + "/Files");


    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    }

    // Create a media file name
    File mediaFile;

    String mImageName="myImage.png";
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName);

    return mediaFile;
}

I tried below with no success. This doesnt create the file. I dont know what is wrong or if im missing something.

ByteArrayInputStream imageStream = new ByteArrayInputStream(imageBlob);
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = imageStream.read(buffer)) != -1) {
      byteBuffer.write(buffer, 0, len);
}
FileOutputStream stream = new FileOutputStream(myPath);
stream.write(byteBuffer.toByteArray());

Need help please. Thanks in advance.

解决方案

 ByteArrayInputStream imageStream = new ByteArrayInputStream(imageBlob);

Once you have that just save the stream to file. Don't make a bitmap of it and don't use BitmapFactory. You can use that for png, jpg, and gif.

这篇关于Android的保存GIF从数据库到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆