Does resource id changes everytime an application starts

前端 未结 3 964
后悔当初
后悔当初 2020-12-17 23:06

I am storing my images in drawable and their resource id in SQLite database.My database is created when the application starts for the first time.

Is it good to save

相关标签:
3条回答
  • 2020-12-17 23:50

    It is Bad idea to store ids, But what is your purpose?? better idea is to store thumbnails to database rather than ids.. this helped me may this example help you do following steps

    1. get thumbnails of your image

      final int thumbnailSize = 128;// define your size

      Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);

    2. Convert it into Byte array

      Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.thumbnail); ByteArrayOutputStream out = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.PNG, 100, out); byte[] buffer=out.toByteArray();

    3. Save it as Blob

      ContentValues cv=new ContentValues(); cv.put(CHUNK, buffer); //CHUNK blob type field of your table long rawId=database.insert(TABLE, null, cv); //TABLE table name

      I havnt used this but this may help you

    0 讨论(0)
  • 2020-12-17 23:55

    the best way was to store image name directly into database and fetch it,then use

    int resID = this.getResources().getIdentifier("your photo name fetched from database","drawable","package name");
    
    image.setResourceID(resID);
    
    0 讨论(0)
  • 2020-12-18 00:05

    One approach would be storing the drawables in strings.xml as a string array something like this:

     <string-array name="location_flags">
        <item>@drawable/ic_image_name</item>
        <item>@drawable/ic_image_name</item>
        <item>@drawable/ic_image_name</item>
        <item>@drawable/ic_image_name</item>
        <item>@drawable/ic_image_name</item>
        <item>@drawable/ic_image_name</item>
        <item>@drawable/ic_image_name</item>
    </string-array>
    

    Then reading this array in your activity code :

    TypedArray locationFlags=getResources().obtainTypedArray(R.array.location_flags);
    

    Then applying the for loop you can get the Drawable something like this:

    for(int i=0i<locationFlags.length();i++)
     {
    
       Drawable drawable = locationFlags.getResourceId(i, -1);
     }
    

    Be sure to recycle the TypedArray after using it, since its a shared resource :

     locationFlags.recycle();
    
    0 讨论(0)
提交回复
热议问题