samedi 4 avril 2015

store images in sqlte from urls

i am trying to insect data into an sqlite database and they are all names, messages and images gotten from mysql database . the problem is how to insect images using Blob.. the images are gotten from a urls. i want to put it in the sqite table.... i know they say is better to save it on the sdcard and put the location in the sqlite table.... but the only problem is that the user would be able to delete some images or rename them which i dont want.


ths is my sqlite database class



package com.mall.our;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.preference.PreferenceManager;

public class DBTool extends SQLiteOpenHelper {
private Context appContext;
static final String firstname="firstname";
static final String lastname="lastname";

public DBTool(Context applicationContext){

super(applicationContext, "friend.db", null, 1);
this.appContext = applicationContext;

}
@Override
public void onCreate(SQLiteDatabase database) {

String query = "CREATE TABLE friends ( contactId INTEGER PRIMARY KEY, name TEXT, " +
"message TEXT, image BLOB)";
database.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {

String query = "DROP TABLE IF EXISTS friends";

database.execSQL(query);
onCreate(database);

}

public void insertContact(HashMap<String, String> queryValues){

SQLiteDatabase database = this.getWritableDatabase();


ContentValues values = new ContentValues();

values.put("name", queryValues.get("name"));
values.put("message", queryValues.get("message"));
values.put("image", queryValues.get("image"));

database.insert("friends", null, values);
database.close();

}

public int updateContact(HashMap<String, String> queryValues){

SQLiteDatabase database = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put("name", queryValues.get("name"));
values.put("message", queryValues.get("message"));
values.put("image", queryValues.get("image"));


return database.update("friends", values,
"contactId" + " = ?", new String[] {queryValues.get("contactId") });

}

public void deleteContact(){

SQLiteDatabase database = this.getWritableDatabase();

String deleteQuery = "DELETE FROM friends ";

database.execSQL(deleteQuery);

}

public ArrayList<HashMap<String, String>> getAllContacts(){

ArrayList<HashMap<String, String>> contactArrayList = new ArrayList<HashMap<String, String>>();


String selectQuery = "SELECT * FROM friends ";
SQLiteDatabase database = this.getWritableDatabase();

Cursor cursor = database.rawQuery(selectQuery, null);


if(cursor.moveToFirst()){

do{

HashMap<String, String> contactMap = new HashMap<String, String>();

contactMap.put("name", cursor.getString(0));
contactMap.put("message", cursor.getString(1));
contactMap.put("image", cursor.getString(2));


contactArrayList.add(contactMap);

} while(cursor.moveToNext());

}
return contactArrayList;

}

public HashMap<String, String> getContactInfo(String id){

HashMap<String, String> contactMap = new HashMap<String, String>();

SQLiteDatabase database = this.getReadableDatabase();

String selectQuery = "SELECT * FROM friends WHERE contactId='" + id + "'";

Cursor cursor = database.rawQuery(selectQuery, null);

if(cursor.moveToFirst()){

do{

contactMap.put("name", cursor.getString(0));
contactMap.put("message", cursor.getString(1));
contactMap.put("image", cursor.getString(2));

} while(cursor.moveToNext());

}

return contactMap;

}

}


this is my class to get the data and insect



package com.mall.our;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import com.actionbarsherlock.app.SherlockListFragment;

import com.mall.first.JSONParser;
import com.mall.first.R;


public class Chat extends SherlockListFragment {
JSONParser jsonParser = new JSONParser();
private static final String TAG_POSTS = "posts";
public static final String TAG_ID = "id";
public static final String TAG_NAME = "name";
public static final String TAG_pic = "pic";
public static final String TAG_MESSAGE = "message";
public static final String TAG_CATEGORIES_LOGO = "categories_logo";
DBTool dbTool = new DBTool(getActivity());
/*
user details
private static final String NAME = "name";
private static final String AGE = "age";
private static final String STATUS = "status";
private static final String PIC = "pic";
private static final String SEX = "sex"; String friendname,status;

private static final String TAG_SUCCESS = "success";*/
//user
private static final String URL_CATEGORY = "http://ift.tt/1FaDCdJ";

private ListView lv;
SharedPreferences sp ;
Bundle bon = new Bundle();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.friends, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

new LoadComments().execute();
}

class LoadComments extends AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());

pDialog.setIndeterminate(false);
pDialog.setCancelable(true);

}

@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
String username = sp.getString("username", "anon");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
JSONObject json = jsonParser.makeHttpRequest(URL_CATEGORY, "POST",
params);

try {

JSONArray categories = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < categories.length(); i++) {
String id = categories.getJSONObject(i).getString("TAG_ID");
String name = categories.getJSONObject(i).getString("TAG_NAME");
String pic = categories.getJSONObject(i).getString("TAG_pic");
String message = categories.getJSONObject(i).getString("TAG_MESSAGE");

HashMap<String, String> queryValuesMap = new HashMap<String, String>();

queryValuesMap.put("name", name);

queryValuesMap.put("message",message);

DefaultHttpClient mHttpClient = new DefaultHttpClient();
HttpGet mHttpGet = new HttpGet(pic);
HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = mHttpResponse.getEntity();
if ( entity != null) {
// insert to database
queryValuesMap.put("image", EntityUtils.toByteArray(entity));

}
}


dbTool.insertContact(queryValuesMap);

}
}catch (Throwable e){
e.printStackTrace();
}
return categoryList;
}

@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);


}
}
}


the problem is from the "queryValuesMap.put("image", EntityUtils.toByteArray(entity));" line is saying "The method put(String, String) in the type HashMap is not applicable for the arguments (String, byte[])"


i dont know if it is the way i arranged it or is there a better way to do it.. go the idea from this site http://ift.tt/1xNFJBM


Aucun commentaire:

Enregistrer un commentaire