0

这是我的场景。

我有一个从 sqlite 表中获取所有行并将它们显示在 ListView 中的活动。表有 5 列,最后一列是 sdcard 上的照片 URI。我需要知道如何让该 URI 显示在我的 Activiy 的 ImageView 上?

这是数据库记录的示例:

sqlite> select * from events;
_id         title       location    date        img_loc                                                        
----------  ----------  ----------  ----------  ---------------------------------------------------------------
1           office      space       today       file:///storage/sdcard0/DCIM/OrangeClubPhotos/1370701231842.jpg
2           home        street26    june        null                                                           
3           wrjjfhwiru  rkljfewlr   487598347   file:///storage/sdcard0/DCIM/OrangeClubPhotos/1370702333785.jpg
4           jojo        jiji        today       file:///storage/sdcard0/DCIM/OrangeClubPhotos/1370702372846.jpg
5           office des  camp        right now   null                                                           
6           bed         no locatio  right here  null                                                           
7           home home   nyc home    June 8      file:///storage/sdcard0/DCIM/OrangeClubPhotos/1370714226736.jpg

这是我的课程,将显示数据:

package com.parspake.OrangeClub;

import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

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

public class History extends ListActivity {

SQLiteOpenHelper mydbH;
SQLiteDatabase mydb;
ArrayList<String> results = new ArrayList<String>();
Cursor c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.history);

    final ListView listView = (ListView) findViewById(android.R.id.list);
    mydbH = new DbHandler(this);
    mydb = mydbH.getReadableDatabase();

    try {
        c = mydb.rawQuery("select * from events;", null);

        if (c != null ) {
            if (c.moveToFirst()) {
                do {
                    int cId = c.getInt(c.getColumnIndex("_id"));
                    String title = c.getString(c.getColumnIndex("title"));
                    String location = c.getString(c.getColumnIndex("location"));
                    String date = c.getString(c.getColumnIndex("date"));
                    String img = c.getString(c.getColumnIndex("img_loc"));
                    results.add(cId + ": " + title + " - " + location + " - " + date + " - " + img);
                } while (c.moveToNext());
            }
        }

        this.setListAdapter(new MyFinalAdapter(this, R.layout.listviewfinal, R.id.label, results));
    } catch (SQLiteException x) {
        x.printStackTrace();
    }
}

private class MyFinalAdapter extends ArrayAdapter {
    public MyFinalAdapter(Context context, int resource, int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = vi.inflate(R.layout.listviewfinal, parent, false);
        TextView tx = (TextView) row.findViewById(R.id.label);
        ImageView iv = (ImageView) row.findViewById(R.id.photoInDb);
        tx.setText(results.get(position));

// so here each listview row in my application is filled by each row of the table.
// but i dont know how to get that final column - image URI - to be opened in my ImageView?

        return row;
    }
   }
}

History 类的 XML 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="History of the posts that are not submitted yet!"
        android:id="@+id/textView" android:layout_gravity="left|center_vertical" android:background="#ffffb7"
        android:autoText="false" android:singleLine="false" android:textStyle="bold"/>
<ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list" android:layout_gravity="left|center_vertical" />
</LinearLayout>

这是我在自定义适配器中使用的自定义列表视图 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:padding="20dp"
        android:layout_height="wrap_content" />
<ImageView
        android:id="@+id/photoInDb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

任何帮助,将不胜感激。

4

1 回答 1

1
String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath() + "/your path here/"
        Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
        ImageView myImageView = (ImageView)findViewById(R.id.imageview);
        myImageView.setImageBitmap(bitmap);
于 2013-06-09T07:47:53.127 回答