如何从db获取存储图像的URI并在图像视图中显示

时间:2021-10-31 00:21:18

I am trying to fetch image URL saved in my local sql dB and display it in the image view but some how there is error on my image being null.. i am already reading name of the image and it works perfect but the image uri cant not be displayed, As the URI is being fetch its in String format so need to b decoded first to be displayed.. is there a way to decode it when my cursor fetch URI and decode it send it in list-View for display.

我正在尝试获取保存在我本地sql dB中的图像URL并在图像视图中显示它,但是我的图像上有一些错误如何为空...我已经在读取图像的名称并且它工作完美但图像不清楚不显示,因为URI正在以字符串格式获取它所以需要先解码才能显示..有没有办法在我的光标获取URI时对其进行解码并解码它将其发送到list-View中进行显示。

please see the code below: ----------->>>>>>>>>>the class where the fetched data is being displayed

请参阅以下代码:----------- >>>>>>>>>>显示提取数据的类

public class NewsFeed_Viewoverlay extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newsfeed_viewoverlay);
        DatabaseHelper helper;
        ImageView list_image;
        VideoView list_video;
        helper= new DatabaseHelper(this.getApplicationContext());
        list_image= (ImageView) findViewById(R.id.list_image);
        list_video = (VideoView)findViewById(R.id.list_video);

//-------------------------------------------------------------------->>>>>>>>XXXXXXXXXXX<<<<<<<<<<-----------------------
        Cursor cursor = helper.getCursor();
        startManagingCursor(cursor);
        //setup for MApping in view fields
        String [] fromFieldNames=new String[]{helper.KEY_NAME,helper.KEY_IMAGE};

       //list_image.setImageBitmap(BitmapFactory.decodeFile(helper.KEY_IMAGE));
       // list_video.setVideoURI(Uri.parse(helper.KEY_FILE));

        int[] toViewID= new int[]{R.id.title, R.id.list_image};
        SimpleCursorAdapter myadaptor= new SimpleCursorAdapter(this, R.layout.list_row, cursor,fromFieldNames,toViewID );
        ListView mylist= (ListView)findViewById(R.id.list);
        mylist.setAdapter(myadaptor);


        Log.i("READ-->Path in DB->", "" + helper.KEY_NAME + helper.KEY_IMAGE);

    }

---------------------->>>>>>>THE Database class for cursor----------------

---------------------- >>>>>>>游标的数据库类----------------

public void Insert(String imgpath, String vidPath, String name ) throws SQLException {

    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_IMAGE, imgpath);
    values.put(KEY_FILE, vidPath);
    values.put(KEY_NAME,name);

    db.insert(TABLE_Images, null, values);
    db.close();
    Log.i("INSERTED-->Path in DB->", "" + imgpath);
    Log.i("INSERTED-->Path in DB->", "" + vidPath);
    Log.i("INSERTED-->Path in DB->", "" + name);

}
    public static final String[] KEY= new String[] {KEY_ID,KEY_NAME,KEY_IMAGE,KEY_FILE};
public Cursor getCursor()
{
    String where=null; ContentResolver resolver;
    db= this.getReadableDatabase();
    //db.rawQuery("select image , name from images",null);
    //Cursor cursor = db.rawQuery("select _id, name from images",null);
    Cursor cursor= db.query(true,TABLE_Images,KEY,where,null,null,null,null,null,null);
    if (cursor!=null)
    {
        cursor.moveToFirst();

    }
    return cursor;

}

please refer any links or at-least tell me if there is alternative.

请参考任何链接或至少告诉我是否有替代方案。

3 个解决方案

#1


0  

After retrieving the URI from database, you need to parse it into URI format.

从数据库中检索URI后,您需要将其解析为URI格式。

Uri mUri = Uri.parse("Data you got from db");

After that you can show it in ImageView.

之后,您可以在ImageView中显示它。

imageView.setImageURI(mUri);

#2


0  

Store the URI as a string in the database and then load it later.

将URI作为字符串存储在数据库中,然后再加载它。

// This will get the uri in a string format

//这将以字符串格式获取uri

String s = mUri.toString();

When you retrieve the string from the database, rebuild the URI like this:

从数据库中检索字符串时,重建URI如下所示:

// This will decode the string into a URI

//这会将字符串解码为URI

Uri mUri = Uri.parse(s);

for more info follow the link here

有关更多信息,请点击此处链接

#3


0  

first store image uri in a String variable(like imagePath). then decode your string variable(imagePath). Then set your imagePath following code:

首先将图像uri存储在String变量中(如imagePath)。然后解码你的字符串变量(imagePath)。然后按照代码设置imagePath:

setImageBitmap(BitmapFactory.decodeFile(imagePath));

setImageBitmap(BitmapFactory.decodeFile(的ImagePath));

#1


0  

After retrieving the URI from database, you need to parse it into URI format.

从数据库中检索URI后,您需要将其解析为URI格式。

Uri mUri = Uri.parse("Data you got from db");

After that you can show it in ImageView.

之后,您可以在ImageView中显示它。

imageView.setImageURI(mUri);

#2


0  

Store the URI as a string in the database and then load it later.

将URI作为字符串存储在数据库中,然后再加载它。

// This will get the uri in a string format

//这将以字符串格式获取uri

String s = mUri.toString();

When you retrieve the string from the database, rebuild the URI like this:

从数据库中检索字符串时,重建URI如下所示:

// This will decode the string into a URI

//这会将字符串解码为URI

Uri mUri = Uri.parse(s);

for more info follow the link here

有关更多信息,请点击此处链接

#3


0  

first store image uri in a String variable(like imagePath). then decode your string variable(imagePath). Then set your imagePath following code:

首先将图像uri存储在String变量中(如imagePath)。然后解码你的字符串变量(imagePath)。然后按照代码设置imagePath:

setImageBitmap(BitmapFactory.decodeFile(imagePath));

setImageBitmap(BitmapFactory.decodeFile(的ImagePath));