如何将从 url 检索到的图像存储在 SQLite 数据库中?
我正在从 url 检索图像。 是否可以将图像存储在 SQLite 数据库中,而不是缓存图像?
/** Simple Constructor saving the 'parent' context. */
public ImageAdapter(Context c) { this.myContext = c; }
/** Returns the amount of images we have defined. */
public int getCount() { return this.myRemoteImages.length; }
/* Use the array-Positions as unique IDs */
public Object getItem(int position) { return position; }
public long getItemId(int position) { return position; }
/** Returns a new ImageView to
* be displayed, depending on
* the position passed. */
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(this.myContext);
try {
URL aURL = new URL(myRemoteImages[position]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
Log.v(imageUrl, "Retrieving image");
/* Apply the Bitmap to the ImageView that will be returned. */
i.setImageBitmap(bm);
} catch (IOException e) {
Log.e("DEBUGTAG", "Remtoe Image Exception", e);
}
/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
/* Set the Width/Height of the ImageView. */
if(Build.VERSION.SDK_INT >= 11){
i.setLayoutParams(new Gallery.LayoutParams(450, 300));
return i;
}
else{
i.setLayoutParams(new Gallery.LayoutParams(125, 125));
return i;
}
}
/** Returns the size (0.0f to 1.0f) of the views
* depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
编辑:设置 imageAdapter 以加载图库中的图像
((Gallery) findViewById(R.id.gallery))
.setAdapter(new ImageAdapter(MainMenu.this));
I am retrieving images from a url.
Instead of caching the images, would it by any chance be possible to store it in a SQLite database?
/** Simple Constructor saving the 'parent' context. */
public ImageAdapter(Context c) { this.myContext = c; }
/** Returns the amount of images we have defined. */
public int getCount() { return this.myRemoteImages.length; }
/* Use the array-Positions as unique IDs */
public Object getItem(int position) { return position; }
public long getItemId(int position) { return position; }
/** Returns a new ImageView to
* be displayed, depending on
* the position passed. */
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(this.myContext);
try {
URL aURL = new URL(myRemoteImages[position]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
Log.v(imageUrl, "Retrieving image");
/* Apply the Bitmap to the ImageView that will be returned. */
i.setImageBitmap(bm);
} catch (IOException e) {
Log.e("DEBUGTAG", "Remtoe Image Exception", e);
}
/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
/* Set the Width/Height of the ImageView. */
if(Build.VERSION.SDK_INT >= 11){
i.setLayoutParams(new Gallery.LayoutParams(450, 300));
return i;
}
else{
i.setLayoutParams(new Gallery.LayoutParams(125, 125));
return i;
}
}
/** Returns the size (0.0f to 1.0f) of the views
* depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
EDIT: Set the imageAdapter to load images in Gallery
((Gallery) findViewById(R.id.gallery))
.setAdapter(new ImageAdapter(MainMenu.this));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将在运行时将您的 imageUrl 转换为 Drawble,然后将 Drawble 设置为 Gallery 的 Imageview
this will convert your imageUrl to Drawble at runtime, then set the Drawble to Imageview of Gallery
是的,您可以将图像作为 BLOB 存储在数据库中,
以获取
//设置
imageView.setImageBitmap(getImageFromBLOB(cursor.getBlob(object.getColumnIndex("book_thumb"))));
ya you can store image as BLOB in your database,
To fetch
// to set
imageView.setImageBitmap(getImageFromBLOB(cursor.getBlob(object.getColumnIndex("book_thumb"))));
以下是如何在外部存储中存储数据的链接:外部存储< /a>
该链接解释了放置文件的位置(如果您想使用外部存储),以及如何检查外部存储是否可用。
编辑:“访问外部存储上的文件”主题下解释了如何访问文件。
您应该在 API 8 或更高版本中调用
getExternalFilesDir()
来获取代表您的应用程序根目录的File
。然后,您可以像平常一样读取和写入文件(例如使用 FileWriter 和 FileReader 来读取文本数据)Here's a link to how you store data in external storage: External storage
The link explains where to place files (if you want to use external storage), and how to check if external storage is available.
Edit: How you access files, is explained under the topic "Accessing files on external storage".
You should in API 8, or above invoke
getExternalFilesDir()
to get aFile
that represents your applications root directory. You can then read and write the files as you normally do (using e.g. FileWriter and FileReader for text data)