简体   繁体   English

如何从资源设置位图

[英]How to set a bitmap from resource

This seems simple, I am trying to set a bitmap image but from the resources, I have within the application in the drawable folder.这看起来很简单,我正在尝试设置位图图像,但是从资源中,我在 drawable 文件夹中的应用程序中。

bm = BitmapFactory.decodeResource(null, R.id.image);

Is this correct?这是正确的吗?

Assuming you are calling this in an Activity class假设您在 Activity 类中调用它

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);

The first parameter, Resources, is required.第一个参数 Resources 是必需的。 It is normally obtainable in any Context (and subclasses like Activity).它通常可以在任何 Context(以及像 Activity 这样的子类)中获得。

Try this试试这个

This is from sdcard这是来自sdcard

ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);

This is from resources这是来自资源

Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

If the resource is showing and is a view, you can also capture it.如果资源正在显示并且是一个视图,您也可以捕获它。 Like a screenshot:就像截图一样:

View rootView = ((View) findViewById(R.id.yourView)).getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.layout(0, 0, rootView.getWidth(), rootView.getHeight());
rootView.buildDrawingCache();

Bitmap bm = Bitmap.createBitmap(rootView.getDrawingCache());

rootView.setDrawingCacheEnabled(false);

This actually grabs the whole layout but you can alter as you wish.这实际上抓住了整个布局,但您可以根据需要进行更改。

If you have declare a bitmap object and you want to display it or store this bitmap object.如果您已经声明了一个位图对象并且您想要显示它或存储这个位图对象。 but first you have to assign any image , and you may use the button click event, this code will only demonstrate that how to store the drawable image in bitmap Object.但是首先您必须分配任何图像,并且您可以使用按钮单击事件,此代码仅演示如何将可绘制图像存储在位图对象中。

Bitmap contact_pic = BitmapFactory.decodeResource(
                           v.getContext().getResources(),
                           R.drawable.android_logo
                     );

Now you can use this bitmap object, whether you want to store it, or to use it in google maps while drawing a pic on fixed latitude and longitude, or to use some where else现在你可以使用这个位图对象,无论你是想存储它,还是在谷歌地图中使用它,同时在固定的经纬度上绘制图片,或者在其他地方使用

just replace this line只需更换这一行

bm = BitmapFactory.decodeResource(null, R.id.image);

with

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.YourImageName);

I mean to say just change null value with getResources() If you use this code in any button or Image view click event just append getApplicationContext() before getResources()..我的意思是说只需使用 getResources() 更改空值如果您在任何按钮或图像视图单击事件中使用此代码,只需在 getResources() 之前附加 getApplicationContext()。

Using this function you can get Image Bitmap.使用此功能您可以获得图像位图。 Just pass image url只需传递图片网址

 public Bitmap getBitmapFromURL(String strURL) {
      try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
      } catch (IOException e) {
        e.printStackTrace();
        return null;
      }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM