需要有关混合&QUOT建议;乌里/ INT ID"周围环境的图像 [英] Need suggestion about a mixed "Uri / int id" images ambient

查看:111
本文介绍了需要有关混合&QUOT建议;乌里/ INT ID"周围环境的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

假设到现在为止,我使用R.drawable.img设置一些imageviews图像

suppose that until now, I am using R.drawable.img to set the image in some imageviews with

imgView.setImage(aProduct.getImgId());


简化的产品类别是这样的:


the simplified product class look like this:

class Product{
        int imgId;

        ....
        public void setImgId(int id){
        this.imgId=id;
        }

        public int getImgId(){
        return this.imgId
        }

        ...




    }

我的应用程序现在已进化,因为用户可以添加定制产品
服用从相机在img和获取图像的URI。
并设置在ImageView的imgView.setImgURI(URI)图像

my application is now "evolved" because the user can add customized products taking the img from the camera and getting the Uri of the picture. and to set the image on the ImageView imgView.setImgURI(Uri)

现在我的问题是:
这将是拥有混合INT /乌里图像资源环境的最佳方法?
我能得到一个R.drawable.img的URI?

Now my question is: what would be the best approach to have a mixed int/Uri image resources ambient? can I obtain the Uri of a "R.drawable.img"?

我不知道如果我的问题是清楚的,我的意思是:

I'm not sure if my question is clear, I mean:

我要检查,之前设置ImageView的,如果我的产品有一个URI或一个int标识,
然后再做一个如果来调用适当的方法,或者是有简单的解决方案?

I have to check, before to set the imageview, if my product has an Uri or an int Id, and then make an "if" to call the appropriate method, or there is a simpler solution?

感谢您的阅读,和对不起我的英语水平。

Thank you for reading, and sorry for my english.

问候。

推荐答案

您的问题是,基本上有3种类型的图像资源:

Your problem is that there are basically 3 types of image resources:


  • R.id ... 资源:内部资源,比如你把res文件夹图标

  • 内容的URI :本地文件或内容提供商的资源,如内容:// 的file:/// SD卡/...

  • 远程文件的URL :网络上的图像,如的http:// ...

  • R.id... resources: internal resources, such as icons you put into the res folder
  • content URI's: local files or content provider resources such as content:// or file:///sdcard/...
  • remote file URL's: images on the web, such as http://...

您正在寻找一种方式来绕过的一个标识符的,可以处理所有的的。我的解决办法是通过周围的字符串:的URI的,或者只是字符串资源的R.id整数presentation任的toString()

You are looking for a way to pass around one identifier that can deal with all three. My solution was to pass around a string: either the toString() of the URI's, or just the string respresentation of the R.id integer.

我用下面的code为例,来处理这个,使与它的code的工作:

I'm using the following code, for example, to deal with this and make the code work with it:

public static FastBitmapDrawable returnAndCacheCover(String cover, ImageRepresentedProductArtworkCreator artworkCreator) {
    Bitmap bitmap = null;
    Uri coverUri = null;
    boolean mightBeUri = false;
    //Might be a resId. Needs to be cached. //TODO: problem: resId of default cover may not survive across versions of the app.
    try {
        bitmap = BitmapFactory.decodeResource(Collectionista.getInstance().getResources(), Integer.parseInt(cover));
    } catch (NumberFormatException e) {
        //Not a resId after all.
        mightBeUri=true;
    }
    if(bitmap==null || mightBeUri){
        //Is not a resId. Might be a contentUri.
        try {
            coverUri = Uri.parse(cover);
        } catch (NullPointerException ne) {
            //Is null
            return null;
        }
    }
    if(coverUri!=null){
        if(coverUri.getScheme().equals("content")){
            //A contentUri. Needs to be cached.
            try {
                bitmap = MediaStore.Images.Media.getBitmap(Collectionista.getInstance().getContentResolver(), coverUri);
            } catch (FileNotFoundException e) {
                return null;
            } catch (IOException e) {
                return null;
            }
        }else{
            //Might be a web uri. Needs to be cached.
            bitmap = loadCoverFromWeb(cover);

        }
    }
    return new FastBitmapDrawable(bitmap);
}

您可能会感兴趣接管逻辑部分。 Ofcourse 覆盖在这里讨论的字符串。

You might be interested to take over the logic part. Ofcourse cover is the string in question here.

忘记 android.resource:// 作为 R.id ... 整数的替代品。索赔兜兜它不工作:的http://groups.google.com/group/android-developers/browse_thread/thread/a672d71dd7df4b2d

Forget android.resource:// as a replacement for the R.id... integer. Claims are going round it does not work: http://groups.google.com/group/android-developers/browse_thread/thread/a672d71dd7df4b2d

要了解如何实施 loadCoverFromWeb ,在其他问题周围看看或者ping我。此Web东西是一种它自己的一个领域。

To find out how to implement loadCoverFromWeb, have a look around in other questions or ping me. This web stuff is kind of a field of it's own.

(对GPLv3的code根据我的应用程序Collectionista:的https: //$c$c.launchpad.net/~pjv/collectionista/trunk

(Based on GPLv3 code out of my app Collectionista: https://code.launchpad.net/~pjv/collectionista/trunk)

这篇关于需要有关混合&QUOT建议;乌里/ INT ID"周围环境的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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