please i am trying to get the image of a url and save it in an sqlite db. and it is not working ... i am convert it to bitmap. i dont really know about it . i have already created the sqlite table and i am using BlOB it works well when i get it from drawable...
this is the class.
package com.example.sqliteimagedemo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
public class SQLiteDemoActivity extends Activity {
ArrayList<Contact> imageArry = new ArrayList<Contact>();
ContactImageAdapter adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DataBaseHandler db = new DataBaseHandler(this);
// get image from drawable
URL url;
try {
url = new URL("http://ift.tt/1wbpuvW");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("GeekOnJava", imageInByte));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// display main List view bcard and contact name
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Reading all contacts from database
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName()
+ " ,Image: " + cn.getImage();
// Writing Contacts to log
Log.d("Result: ", log);
// add contacts data in arrayList
imageArry.add(cn);
}
adapter = new ContactImageAdapter(this, R.layout.screen_list, imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
}
}
Aucun commentaire:
Enregistrer un commentaire