从从SQLite数据库获取的URL下载图像的问题

时间:2017-01-03 05:08:05

标签: java android sqlite

我正在Android Studio中创建Android应用程序。我一路上遇到了一些障碍。我正在使用一个非常标准的ListView,它可以添加和删除项目 - 这个位工作正常。为了将数据存储在用户手机上(并且因为我还没有自己的服务器),我选择将数据存储在用户的手机上。

为了存储应用程序的数据,我使用以下教程创建了一个SQLite数据库,但更改了一些列名并添加了列以满足我的需要http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

我创建的其中一个列包含电影海报图片的URL。我的对象类下载此图像并将其设置为Drawable - 这一点也完美无缺。我能够成功地将URL存储在数据库中,并在重新加载应用程序时正确地将其拉出。但是,当我重新加载应用程序时,以前在ListView中的项目不会下载图像,即使正确的URL传递给Object类。请帮我看看我哪里出错了。下面的代码。提前谢谢!

Database Helper(我在那里CRUD SQLite数据库)

public class DatabaseHandler extends SQLiteOpenHelper{
//database version
private static final int DATABASE_VERSION = 1;
//database name
private static final String DATABASE_NAME = "moviesManager";
//table name
private static final String TABLE_MOVIES = "movies";
//table column names
private static final String KEY_ID = "idSTRING";
private static final String KEY_TITLE = "Title";
private static final String KEY_YEAR = "Year";
private static final String KEY_RATING = "Rating";
private static final String KEY_POSTER = "URL";

public DatabaseHandler(Context context){
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
}

//creating tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_MOVIES_TABLE = "CREATE TABLE " + TABLE_MOVIES + "("
            + KEY_ID + "STRING PRIMARY KEY," + KEY_TITLE + " TEXT,"
            + KEY_YEAR + " TEXT," + KEY_RATING + " TEXT,"
            + KEY_POSTER + " TEXT" + ")";
    db.execSQL(CREATE_MOVIES_TABLE);
}

//upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MOVIES);
    //create table again
    onCreate(db);
}

//CRUD (Create, Read, Update, Delete) Operations
//add new movie
void addMovie(Movie movie){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_ID, movie.getId());
    values.put(KEY_TITLE, movie.getTitle());
    values.put(KEY_YEAR, movie.getYear());
    values.put(KEY_RATING, movie.getRating());
    values.put(KEY_POSTER, movie.getImgUrl());
    //Log.e("poster:", values.get(KEY_POSTER).toString());

    //Inserting row
    db.insert(TABLE_MOVIES, movie.getId(), values);
    //Log.e("Poster in DB: ",getMovie(movie.getId()).getImgUrl());
    db.close(); //closing database connection
}

public List<Movie> getAllMovies(){
    List<Movie> movieList = new ArrayList<>();
    String selectQuery = "SELECT  * FROM " + TABLE_MOVIES;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    //looping through all rows and adding to list
    if(cursor.moveToFirst()) {
        do{
            Movie movie = new Movie();
            movie.setId(cursor.getString(0));
            movie.setTitle(cursor.getString(1));
            movie.setYear(cursor.getString(2));
            movie.setRating(cursor.getString(3));
            movie.setPoster(cursor.getString(4));
            movieList.add(movie);
        }while(cursor.moveToNext());
    }
    //cursor.close();
    return movieList;
}
  ...
}

来自MainActivity:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

     ...

    lv = (ListView) findViewById(R.id.list);
    movies = db.getAllMovies();
    mAdapter = new MovieAdapter(this, movies);
    lv.setAdapter(mAdapter);

    ...
}

来自Movie class:

...
public void setPoster(String imgUrl) {
    this.imgUrl = imgUrl;
    this.poster = LoadImageFromWebOperations(this.imgUrl);
}

private static Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        return Drawable.createFromStream(is, "src name");
    } catch (Exception e) {
        Log.e("err","poster not found at "+url);
        return null;
    }
}

如果需要更多信息来回答我的问题,请告诉我。

1 个答案:

答案 0 :(得分:1)

正如riggaroo建议使用库来完成这些任务非常有用,因为它们异步加载图像并管理内存。

我个人建议使用毕加索来完成这项任务。

你只需要导入picasso lib并添加这行代码来加载图像。

Picasso.with(this).load("YOUR IMAGE URL HERE")into(imageView);

用于导入库以及更多用法,请参阅here Picaso Guide