在绘制文件夹中存储的图像和获取图像通过使用数据库中的ImagePath加载 [英] Storing images in drawable folder and getting the image to load by using the imagePath in database

查看:116
本文介绍了在绘制文件夹中存储的图像和获取图像通过使用数据库中的ImagePath加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何获取存储在Eclipse中的文件夹绘制图像的的ImagePath。所述的ImagePath是要被存储在SQLite数据库。所以现在我想知道我怎么检索数据库中的ImagePath的URI。任何帮助将大大AP preciated。谢谢你。

I am wondering how to get the imagePath of the image stored in the drawable folder in eclipse. The imagePath is to be stored in SQLite Database. So now I am wondering how do I retrieve the Uri of the imagePath in the database. Any help will be greatly appreciated. Thank you.

ImagePage.java

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

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编码。

I have updated the coding for the ImagePage.java again.

推荐答案

任何图像资源应放置在绘制所以它不是关于路径,你只需要图像名称被保存在相关的一些实体数据库和通过获取它从数据库中你可以得到通过code中的图像资源低于

any image resource should be placed in drawable so its not about path you just need the image name to be saved in database related to some entity and the by getting it from database you can get the image resource through the code below

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

更新:

就像现在从您根据code质疑你试图打通名称(路径)图像意味着不是整数数组整数[] = imageIDs {R.drawable.pic1,R. drawable.pic2,R.drawable.pic3,R.drawable.pic4};
要通过数据库指出图像资源,要做到这一点首先,你必须得到所有图像名(同绘文件夹的图像的名字的名字),你的数据库保存在一个表中的列一旦你得到通过查询然后,而不是通过整数数组名称引用刚刚从数据库中的名称将允许您从绘图资源访问图像资源,IM假定图像名称通过下列方式存储的ArrayList&LT;弦乐&GT; imageNames =新的ArrayList&LT;串GT;();
//因此,在光标的while循环从游标retriving值时
imageNames.add(cursor.getString(参数:columnIndex));

as now from your code according to question you were trying to get images through name(path) means instead of Integer array Integer[] imageIDs = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4}; you want image resources to be pointed through database so to do this first you have to get all image names(the names same as the name of images in drawable folder) you saved in column of a table in database once you got the names through query then instead of reference through Integer array just the names from database will allow you to access the image resources from drawables, im supposing that image names are stored by following way ArrayList<String> imageNames = new ArrayList<String>(); //hence in the while loop of cursor during retriving values from cursor imageNames.add(cursor.getString(columnIndex));

所以更新code会喜欢下面,其中

so the updated code will like below where

@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;
        }

如果你还在糊涂那么有没有其他的解决方案,我想我能做到

if you still confused then there is no other solution i think i can do

这篇关于在绘制文件夹中存储的图像和获取图像通过使用数据库中的ImagePath加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆