Android SQLite图像存储 [英] Android SQLite image Storage

查看:116
本文介绍了Android SQLite图像存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个测验应用程序,要求我能够从数据库中获取图像BLOB或存储在数据库中的图像路径。但是我环顾四周,很多人建议使用文件路径,问题是如果我使用文件路径方法,我不知道在哪里存储图像。



<我是否将它存储在应用程序中的某个位置,例如资源文件夹?,很多示例使用SD卡但是可以将图像从数据库保存到SD卡,如果是这样,那么这意味着我有两个图像一个在数据库中,一个在SD卡上。



哪里是存储测验应用程序图像的最佳位置,我可以在任何手机上使用,可以访问所述图像?怎么样?。



提前致谢。

解决方案

图像存储必须在某个目录中执行,并且图像的相应路径必须存储在数据库中。





有时你会从一个角度访问你的图像然后另一个,在这种情况下你只需要将图像的路径从活动一传递到活动二和然后从目录中检索图像以在活动二中显示。
当图像大小开始增加时,从数据库存储和加载图像可能会很痛苦。
了解如何存储图片,请注意



代码示例



  private void SaveImage(Bitmap finalBitmap){

String root = Environment.getExternalStorageDirectory()。toString();
File myDir = new File(root +/ saved_images);
myDir.mkdirs();
随机生成器= new Random();
int n = 10000;
n = generator.nextInt(n);
String fname =Image - + n +。jpg;
文件文件=新文件(myDir,fname);
if(file.exists())file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG,90,out);
out.flush();
out.close();

} catch(例外e){
e.printStackTrace();
}

}



在上面的代码中,行

  Environment.getExternalStorageDirectory()

引用 android / data 文件夹。您可以在任何级别内创建文件夹,例如 android / data / folderone / folderTwo / folderThree



注意:然而,您需要先从服务器首先获取图像并将其存储在设备中。如果您正在考虑捆绑将图像与应用程序一起放入res / drawable文件夹中(如果不涉及Web服务器功能)





I am making a quiz app which requires me to be able to get an image from a database BLOB or image path stored in the database. However i have looked around and a lot of people suggest using a file path, the problem is i don't know where to store the image if i use the file path method.

Do i store it somewhere in the app such as the resources folder?, a lot of examples use SD cards but is it possible to save an image to SD card from a database and if so surely that would mean i have two images one in database and one on SD card.

Where is the best place to store a images for a quiz app that i can use on any phone an will have access to said images? and how ?.

Thanks in advance.

解决方案

Image storage must be performed in some directory and the corresponding paths of the image must be stored in the database.

There will be times when you will accessing your images from one acivity then the other, in that case you will just need to pass the path of the image from activity one to activity two and then retrieve the image from the directory to display in activity two. Image storing and loading from databases may turn out to be a pain when the size of the images will start increasing. For learning how to store images, Give an eye to this

CODE EXAMPLE

private void SaveImage(Bitmap finalBitmap) {

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");    
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

}

In the above code, the line

 Environment.getExternalStorageDirectory()

is refering to android/data folder. you can create folder inside upto any level, like android/data/folderone/folderTwo/folderThree .

Note: However you need to first fetch the images from server for the first time and store them in device.If you are thinking of bundling up the images along with the app, put all of your images in res/drawable folders.(if no web server functionality is involved)


这篇关于Android SQLite图像存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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