这是我的情景。
我有一个活动,它从sqlite表中获取所有行并在ListView中显示它们。 table有5列,最后一列是sdcard上的照片URI。我需要知道如何在我的Activiy的ImageView上显示该URI?
这是db记录的示例:
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>
这是我在自定义适配器中使用的自定义listview 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>
任何帮助将不胜感激。
答案 0 :(得分:1)
String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath() + "/your path here/"
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView myImageView = (ImageView)findViewById(R.id.imageview);
myImageView.setImageBitmap(bitmap);