2

I am building android projects with another project as a library. In the library project, it handles all creation method, the other projects refer to that library contains their own resources, like images.

There is a imageView in the library project, what I want to do is to pass the image to the library project to render it, however, I can't just pass the id because two projects have their own resources. I tried to make a Drawable in the child project, and pass to the parent project, it turns out nullpointerexception.

What I can think of is to pass bitmap, but don't know how to create a bitmap using images in the project folder, because all resources are compressed, and I can only use

context.getResources()

to retrieve the resource files.

Or are there some other ways to do this?


Updates:

Sorry guys, the NullPointerException is because I tried to use imageview before it calls

super.onCreate();

Now it throws no exceptions and can be run, but the imageview still can't display the image. My code:

ImageView imageview = (ImageView) findViewById(library.R.id.imageView1);
imageview.setImageResource(R.drawable.image);

Solution

It turns out that I tried to use imageview in the parent class before calling

super.onCreate()

and the image doesn't load because I

setContentView()

which covers the parent Activity's layout.

You can use the methods provided below to pass bitmap, or drawable to the imageview in the super class,

or create an imageview in the child class, then use the libraryPath.R to get the imageview in the parent class.

Kyle Xie
  • 722
  • 1
  • 8
  • 28

4 Answers4

0

Pass the context of your main project to the library project as a parameter of a method.

I think it is better to pass the bitmap to the library project instead of the context or resources.

Bob
  • 22,810
  • 38
  • 143
  • 225
0

You can have any number of library project, but once you compile and run your app it will be a single mass of code and resources, there is nothing that distinguishes a class with an ImageView in the library project from a class in your main project with an ImageView.

Post some code showing where this ImageView is located. What class is it in and in what way are you using it?

Once you have access to the ImageView you can use any of the following methods:

setImageBitmap():

ImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.image));

setImageResource()

ImageView.setImageResource(R.drawable.image);

setImageDrawable()

ImageView.setImageDrawable(getResources().getDrawable(R.drawable.image));
britzl
  • 10,132
  • 7
  • 41
  • 38
  • I tried all methods that you proposed...The all throw NullPointerException. Let me show you some code at the original post. – Kyle Xie May 22 '13 at 06:31
0

I was also facing same problem and there is very rude solution for it. You are getting NullPointerException because I think you are using Switch case in your Library project

according to Android library project implementation guide, use if-else instead of switch case in your library project.

You can directly convert switch case to if-else by a single command. You can see below article for more details.

http://www.tekritisoftware.com/android-library-projects-to-use-common-source-codes

Vivek Kumar Srivastava
  • 2,158
  • 1
  • 16
  • 23
0

private Bitmap imageOriginal, imageScaled;

if (imageOriginal == null) { imageOriginal = BitmapFactory.decodeResource(context.getResources(), drawable_id); }

    image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // method called multiple times but we can initialize just once
            if (imageHeight == 0 || imageWidth == 0) {
                imageHeight = image.getHeight();
                imageWidth = image.getWidth();
                // resize the image
                Matrix resize = new Matrix();
                resize.postScale((float) Math.min(imageWidth, imageHeight) / (float) imageOriginal.getWidth(), (float) Math.min(imageWidth, imageHeight) / (float) imageOriginal.getHeight());
                imageScaled = Bitmap.createBitmap(imageOriginal, 0, 0, imageOriginal.getWidth(), imageOriginal.getHeight(), resize, false);
                // translate the matrix to the image view's center
                float translateX = imageWidth / 2 - imageScaled.getWidth() / 2;
                float translateY = imageHeight / 2 - imageScaled.getHeight() / 2;
                matrix.postTranslate(translateX, translateY);
                image.setImageBitmap(imageScaled);
                image.setImageMatrix(matrix);
            }
        }
    });

This change an image type to Bitmap. i hope it will help you.

developer
  • 1
  • 1