1

I am new for Android. I want to create an jar file if i call a method of that jar file in another android application it will show an image in imageview of the application. For example a method in jar file as DisplayImg(URL); i will use this jar as library in another application and here i will create an imageview

ImageView imageView;
imageView = (ImageView) findViewById(R.id.img);

Then if i call imageView.DisplayImg(URL); then it will display specified url image on the imageview. I spend lot more time to get the solution but not work.plz help with the code thank

user3555513
  • 61
  • 1
  • 6

1 Answers1

2

You don't need to create a library, in fact, there is a same library available for doing exactly what you want to! It's called Picasso. You can find it here.

Also note that if you want imageView.DisplayImage(URL); you should create a new instance of ImageView class, because it doesn't contain this method. I recommend using DisplayImage(URL, imageView);.

But in case you REALLY WANT to do this, here you go:

1- For creating a library, create a New Android Application Project, and in the second window, check Mark this project as a library option. Also notice that we don't need Activity.

2- Create a new class, such as ImageUtils.java in the newly-created project. Here you need to put the DisplayImage() method:

    public static void DisplayImage(String URL, ImageView imageView) {
        new DownloadImageTask(imageView).execute(URL);
    }

And this for the DownloadImageTask class :

public static class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

    ImageView imageView;

    public DownloadImageTask(ImageView bmImage) {
        this.imageView = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap imageBitmap = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            imageBitmap = BitmapFactory.decodeStream(in);
        } catch (IOException e) {
            // Probably there's no Internet connection. Warn the user about checking his/her connection.
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return imageBitmap;
    }

    protected void onPostExecute(Bitmap result) {
        if (imageView != null) imageView.setImageBitmap(result);
    }

}

3- Execute your project as an android application.

4- There should be a jar file under the bin folder. It's your library. Import it to your other applications and use it. In that applications you can use it with this code:

    ImageView imageView = (ImageView) findViewById(R.id.img);
    ImageUtils.DisplayImage(URL, imageView);

5- Don't forget to add this permission to the apps that use this library:

<uses-permission android:name="android.permission.INTERNET" />

Hope it helps :)

Amin
  • 1,034
  • 10
  • 17