2014-10-27 74 views
0

在我的应用程序的数据库中,我已经存储了一个GIF图像作为BLOB。我如何从数据库中检索这个gif,并将其保存为sdcard作为gif文件?对于PNG和JPEG,我知道如何去做。我怎样才能正确实现这个没有任何图书馆? 对PNG和JPEG我做这样的代码:Android从数据库保存GIF文件

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; 
} 

我没有成功尝试以下。这不会创建该文件。我不知道什么是错,或者如果我错过了什么。

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()); 

需要帮助。提前致谢。

+0

什么是数据库中的图像格式? – Henry 2014-10-27 12:38:45

+0

gif在数据库中存储为BLOB。我想检索它并保存到文件。 – user3065901 2014-10-27 13:05:52

+0

是的,但是如何在该blob中编码像素? – Henry 2014-10-27 15:25:24

回答

1
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageBlob); 

一旦你有,只需将流保存到文件。不要制作它的位图,也不要使用BitmapFactory。你可以将它用于png,jpg和gif。

+0

查看更新后的问题 – user3065901 2014-10-27 16:57:29