如何存储或保存,并在本地检索多个远程图像 [英] How to store or save and retrieve multiple remote images locally
问题描述
我要寻找一个在Android的解决方案来存储远程图像(从符检索到的URL)插入本地(SD卡或sqlite的分贝或高速缓存),使图像可以在离线模式下也可以显示。
因为我要在通话过程中,每次显示图片。
I am looking for a solution in Android to store remote images (URLs retrieved from webservices) into the local (SDcard or Sqlite db or cache) so the images can be displayed in offline-mode too. because I want to display each time a picture during a call.
是否有任何解决方案?
推荐答案
请按照下列步骤操作(只有一个图像检索一次):
Please follow these steps (for only one image retrieval at a time):
- 从URL中检索图像作为位图。
- 转换位图到字节数组中。
- 存储字节数组到的SQLite数据库表的BLOB字段。
- 从SQLite的数据库表获取一滴。
- 转换的Blob字节数组。
- 转换字节数组转换为位图。
- 最后,设置位图作为图像到活动图像映射。
公共类ImageStoreAndRetrievalActivity延伸活动{
public class ImageStoreAndRetrievalActivity extends Activity {
private ImageView imageView;
private DatabaseHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_store);
imageView = (ImageView)findViewById(R.id.imageView1);
dbHandler = new DatabaseHandler(getApplicationContext());
new MyThread("http://www.fnordware.com/superpng/pnggrad8rgb.png");
imageView.setImageBitmap(dbHandler.getBitmap(1)); // here 1 is id for the image into tbl_blob table in SQLite DB.
}
public class MyThread implements Runnable {
private String url;
private Bitmap bitmap = null;
private Thread th;
public MyThread(String url) {
this.url = url;
th = new Thread(this);
th.start();
try {
th.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
URL fileUrl = null;
try {
fileUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) fileUrl.openConnection();
conn.setConnectTimeout(0);
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
dbHandler.insertBitmap(bitmap);
Log.i("URL", url);
}
}
}
public static final String DATABASE_NAME = "blobdb";
public static final int DATABASE_VERSION = 1;
public static final String CREATE_BLOB_TABLE = "CREATE TABLE IF NOT EXISTS tbl_blob " + "(id INTEGER PRIMARY KEY AUTOINCREMENT, img BLOB NOT NULL, description TEXT NULL)";
公共类数据库处理器扩展SQLiteOpenHelper
{
私人上下文的背景下;
public class DatabaseHandler extends SQLiteOpenHelper { private Context context;
public DatabaseHandler(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db)
{
// create all tables in 'btlocdb'
createTables(db);
}
/** create all tables in 'btlocdb'.
* @param db
*/
public void createTables(SQLiteDatabase db) {
db.execSQL(CREATE_BLOB_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
//db.execSQL("DROP TABLE IF EXISTS " + Consts.Table.DATABASE_NAME);
// Create tables again
//onCreate(db);
}
public void insertBitmap(Bitmap bm)
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
byte[] buffer=out.toByteArray();
SQLiteDatabase db = this.getWritableDatabase();
// Starts the transaction.
db.beginTransaction();
ContentValues values;
try
{
values = new ContentValues();
values.put("img", buffer);
values.put("description", "This is a description");
// Inserting Row
long i = db.insert("tbl_blob", null, values);
Log.i("Insert", i + "");
// Insert or update data into database successfully.
db.setTransactionSuccessful();
}
catch (SQLiteException e)
{
e.printStackTrace();
}
finally
{
db.endTransaction(); // Ending the transaction.
db.close(); // Closing database connection
}
}
public Bitmap getBitmap(int id){
Bitmap bm = null;
SQLiteDatabase db = this.getWritableDatabase();
// Starts the transaction.
db.beginTransaction();
try
{
String selectQuery = "select * from tbl_blob where id = " + id;
Cursor cursor = db.rawQuery(selectQuery, null);
Log.i("Cursor", cursor.getCount() + "");
if(cursor.getCount() > 0)
{
if (cursor.moveToFirst()) {
do {
byte[] blob = cursor.getBlob(cursor.getColumnIndex("img"));
ByteArrayInputStream imageStream = new ByteArrayInputStream(blob);
bm = BitmapFactory.decodeStream(imageStream);
} while (cursor.moveToNext());
}
}
db.setTransactionSuccessful();
}
catch (SQLiteException e)
{
e.printStackTrace();
}
finally
{
db.endTransaction(); // Ending the transaction.
db.close(); // Closing database connection
}
return bm;
}
}
希望这会帮助你!
让我知道如果您有任何关于此问题的任何查询。谢谢..
Hope this will help you!!! Let me know if you have any query regarding this issue. Thanks..
这篇关于如何存储或保存,并在本地检索多个远程图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!