0

我想知道如何获取存储在 eclipse 中可绘制文件夹中的图像的 imagePath。imagePath 将存储在 SQLite 数据库中。所以现在我想知道如何在数据库中检索 imagePath 的 Uri。任何帮助将不胜感激。谢谢你。

ImagePage.java

package main.page;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class ImagesPage extends Activity 
{
    Integer[] imageIDs = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4};
    ImageAdapter imgDB = new ImageAdapter(this);

    ArrayList<String> imageNames = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image);

        Button btnNext = (Button)findViewById(R.id.buttonNext);
        btnNext.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {
            Intent i = new Intent(ImagesPage.this, ImagesSecondpage.class);
            startActivity(i);

            }
        });


        GridView gridView = (GridView)findViewById(R.id.gridview);
        gridView.setAdapter(new ImgAdapter(this));      

        gridView.setOnItemClickListener(new OnItemClickListener()
        {

                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id)
                {
                    imgDB.open();
                    long _id = imgDB.insertImage("pic" + (position + 1)+ ".jpg");

                    imgDB.close();
                }
        }); 
    }

    public class ImgAdapter extends BaseAdapter
      {
          private Context context;

          public ImgAdapter(Context c)
          {
              context = c;

          }


        @Override
        public int getCount()
        {
        return imageIDs.length;
        }

        @Override
        public Object getItem(int position)
        {
            return position;
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView ,ViewGroup parent)
        {
          ImageView imageView;
          if(convertView == null)
          {
              imageView = new ImageView(context);
              imageView.setImageResource(getResources().getIdentifier(imageNames.get(position), defType, defPackage))
              imageView.setLayoutParams(new GridView.LayoutParams(85,85));
              imageView.setScaleType(ImageView.ScaleType.FIT_XY);
              imageView.setPadding(5, 5, 5, 5);

          }else
          {
              imageView = (ImageView) convertView;
          }
            imageView.setImageResource(imageIDs[position]);
            return imageView;


        }

     }

}

ImageAdapter.java

    package main.page;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ImageAdapter
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "img_name";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "anniversary";
    private static final String DATABASE_TABLE = "image";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE = "create table image (_id integer primary key autoincrement, "+ "img_name text not null);";
    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public ImageAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
          try
          {
            db.execSQL(DATABASE_CREATE);
          }catch(SQLException e)
          {
              e.printStackTrace();
          }

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version" +oldVersion + "to" + newVersion +", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS image");
            onCreate(db);   
        }



    }


    public ImageAdapter open() throws SQLException
    {
        db = DBHelper.getReadableDatabase();
        return this;
    }

    public void close()
    {
        DBHelper.close();
    }

    public long insertImage(String img_name)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, img_name);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean deleteImage(long rowId)
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID +"="+ rowId, null) > 0;
    }

    public Cursor getAllImages()
    {
        return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME}, null, null, null, null, null);
    }

    public Cursor getImage(long rowId) throws SQLException
    {
        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME}, KEY_ROWID +"="+ rowId, null, null, null, null, null);
        if(mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public boolean updateImage(long rowId, String img_name)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_NAME, img_name);
        return db.update(DATABASE_TABLE, args, KEY_ROWID +"="+ rowId, null) > 0;
    }

}

我再次更新了 ImagePage.java 的编码。

4

2 回答 2

1
String path = getResources().getString(R.drawable.icon);
于 2012-07-27T09:11:35.287 回答
0

任何图像资源都应该放在drawable中,因此它与路径无关,您只需要将图像名称保存在与某个实体相关的数据库中,通过从数据库中获取它,您可以通过下面的代码获取图像资源

Bitmap image = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier( "imagename" , "drawable", getPackageName()));

更新:

现在从您的代码中根据问题您试图通过名称(路径)获取图像意味着Integer[] imageIDs = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4}; 您希望通过数据库指向图像资源而不是整数数组,因此首先您必须获取所有图像名称(名称与可绘制文件夹中的图像名称)一旦通过查询获得名称,您就保存在数据库中表的列中,而不是通过整数数组引用,仅数据库中的名称将允许您从可绘制对象访问图像资源,我假设图像名称通过以下方式存储ArrayList<String> imageNames = new ArrayList<String>(); //hence in the while loop of cursor during retriving values from cursor imageNames.add(cursor.getString(columnIndex));

所以更新的代码将如下所示

@Override
        public View getView(int position, View convertView ,ViewGroup parent)
        {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(getResources().getIdentifier(imageNames.get(position), "drawable", this.getPackageName()));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
            imageView.setBackgroundResource(itemBackground);


            return imageView;
        }

如果您仍然感到困惑,那么我认为我没有其他解决方案

于 2012-07-27T09:14:23.350 回答