【发布时间】:2014-04-06 09:19:38
【问题描述】:
我有图片存储在 SD 卡上。
这是路径。
file:///mnt/sdcard/2014-04-06%2010%3A49%3A20.jpg
这些图像路径存储在 sqlite db 中。我有一个方法可以将这些路径作为列表返回。
我为显示图像的活动制作了一个带有网格视图的布局文件。 我还制作了带有图像视图小部件的布局文件。我想创建一个 baseadapter 来扩充这个布局文件。 这里列出主要活动。
?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/grid_view"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth"
>
</GridView>
这是baseadapter布局文件的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"/>
</LinearLayout
以下是主要图像活动的代码
public class ImagesActivity extends Activity {
private ImagesAdapter adapter;
List<Incidentmages> images= new ArrayList<Incidentmages>();
DBHelperClass db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
db= new DBHelperClass(this);
setContentView(R.layout.incident_images);
Intent i=getIntent();
String myid=i.getStringExtra("IncidentID");
Log.e("id", myid);
int _id= Integer.parseInt(myid);
// get image paths in a list
images=db.GetIncidentImages(_id);
adapter= new ImagesAdapter(this,R.layout.images_display,images);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(adapter);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
db.close();
super.onStop();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
db.close();
super.onPause();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
db.close();
super.onDestroy();
}
}
这是baseadapter的代码
public class ImagesAdapter extends BaseAdapter {
private Context context1;
int layoutid;
List<Incidentmages> images= new ArrayList<Incidentmages>();
public ImagesAdapter(Context context, int resourcelayouteid,
List<Incidentmages> images
) {
this.context1=context;
this.layoutid=resourcelayouteid;
this.images=images;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return images.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View row = view;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater=
LayoutInflater.from(parent.getContext());
row =inflater.inflate(layoutid,parent,false);
holder = new RecordHolder();
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag(); }
Incidentmages d= (Incidentmages)images.get(position);
String path= d.getPath();
String mypath= path.substring(5,path.length());
String FKID= Integer.toString(d.getIncident_id());
Log.e("Imageadapter", path);
Log.e("FK",FKID);
Bitmap bm= BitmapFactory.decodeFile(mypath);
holder.imageItem.setImageBitmap(bm);
return row;
}
static class RecordHolder {
ImageView imageItem;
}
}
你可以看到我使用 BitmapFactory 来设置图像位图......
我得到的字符串看起来像
///mnt/sdcard/2014-04-06%2010%3A49%3A20.jpg
现在的问题是图像不显示。我得到一个空白屏幕。
我的代码有什么问题吗?
也许问题是我得到路径的方式?
下面是我如何从 URI 中获取路径字符串
{
SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date mydate = new Date();
String imagenumber= fm.format(mydate) + ".jpg";
File file = new File(Environment.getExternalStorageDirectory(),
imagenumber);
Uri outputFileUri = Uri.fromFile(file);
Intent picintent= new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
picintent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(picintent,TAKE_PHOTO);
myPaths.add(outputFileUri.toString());
}
【问题讨论】:
-
您可能想要添加的任何错误?
-
我没有收到错误。我已调试并注意到所有变量都已正确填充。
标签: android