【问题标题】:Display images stored on sd card on gridview在gridview上显示存储在sd卡上的图像
【发布时间】: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


【解决方案1】:

您需要转义空格。尝试将“”替换为“\”

mypath.replace("%20", "\\ ");

编辑:这里的第二个参数中应该有两个反斜杠。一个在降价中被消耗掉了。我已经更正了。

试试吧,应该可以的。

编辑:您的文件路径中有%3A。不确定,将它们替换为 = 并且您的图像路径应该可以工作。

mypath.replace("%3A","=")

引用自另一个关于 filenames with whitespaces in android 的 SO 问题

【讨论】:

  • 试了一下得到了 ///mnt/sdcard/2014-04-06\2010\3A49\3A33.jpg 也没有打开图片
  • 我已经编辑了我的答案。你SD卡里的文件名是2014-04-06 10=49=20还是2014-04-06 10-49-20
  • 你好..你看到我在主线程中关于如何获取文件路径的编辑了吗?可能是错的?
  • 所以你不再从数据库中获取路径了?
  • 在 sd 上检查它们看起来像 IMG_20140406_161206.jpg
【解决方案2】:

问题在于我保存路径的方式!...

我添加了获取图像时获取文件路径的代码。

@Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent resultData) {
        super.onActivityResult(requestCode, resultCode, resultData);

            if (resultData != null) {

            String[] projection = { MediaStore.Images.Media.DATA };
                    @SuppressWarnings("deprecation")
                    Cursor cursor = managedQuery(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                            projection, null, null, null);
                    int column_index_data = cursor
                            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    cursor.moveToLast();

                    String imagePath = cursor.getString(column_index_data);
                    // add image path
                   myPaths.add(imagePath);
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多