Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Holly Fox
2,808 PointsHow do I import images into Android Studio?
So perhaps I'm running before I can walk, but I have a query that hopefully will be easy to resolve.
I've just finished watching Ben's course to develop a simple Fun Facts app in android. I would like to make my own using the same principals shown but with some subtle differences to see if the theory has sunk in. Instead of a text label I would like to use images.
I tried using the design layout and used ImageView. Now I'm stumped. How do I import an image?
I'm just using random JPEG images found on the internet which I have saved to my desktop.
How do I show them in the layout and java classes?
4 Answers

Gunjeet Hattar
14,483 PointsOk finally, it's working
So what I did was stored two images inside drawable-mdpi folder (image01 and image02) for test purposes.
Set up an image view and a button in layout with their respective id's
In the main activity I found their source through findViewByID
For the image I created a Drawable array, since that is what image view will look for to set the images and initialized it to null.
If you notice I initalized the drawables inside onCreate and after appImageView . If you will initialize it before the getResources will return a null since ImageView does not exist until then.
Finally inside the button's onClick method use the random, the integer and finally use the setImageDrawable method to set the images
Hope it made sense :)
If you have any question feel free to ask
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;
public class DailyImageActivity extends Activity {
private ImageView appImageView;
private Button appButton;
private Drawable drawable;
private Random random;
private Drawable [] drawables = null; // create a Drawables array that stores location of different images
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily_image);
appImageView = (ImageView) findViewById(R.id.imageView);
appButton =(Button) findViewById(R.id.button);
/*
* Store the location of images inside the array
*/
drawables = new Drawable[] {
getResources().getDrawable(R.drawable.image01),
getResources().getDrawable(R.drawable.image02)
};
appButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
random = new Random();
int randomNumber = random.nextInt(drawables.length);
drawable = drawables[randomNumber];
appImageView.setImageDrawable(drawable); // set the image to the ImageView
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.daily_image, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Gunjeet Hattar
14,483 PointsFirst I would recommend just going through in brief of this article Supporting Different Screen Sizes - Android
By default images go in your drawable directory. For each device type there is a separate folder (drawable-hdpi, drawable-mdpi, etc.). Currently there is a bug in Android studio if you simply drag and drop the images to the folder it will throw an error. As a turn around you can simply go to the folder location in your computer and copy paste the image in the respective drawable folder. If you are using only for testing purposes put your images in drawable-mdpi folder. No specific reason, just that it corresponds to the middle point of screen sizes .
So what I can infer from your idea is that you want to change the image each time the user presses the button.
Well following the same steps as given by Ben, this is what I can suggest
1) Add a Image View to your design layout
2) Download a number of images and store it inside drawable-mdpi folder. (Suggestion - for consistent viewing make sure images are of same size)
3) Instead of storing "facts" insde the String array , you could store each image location as an array. (just a thought)
4) Fetch the location each time the user clicks the button and update the ImageView with the new image stored at that location.
5) Randomize the choice so produce different images
Hope I made sense

Holly Fox
2,808 PointsHi Gunjeet
Thank you, yes that makes sense but just hit a stumbling block putting it into practice, hope you don't mind if I pick your brain further?
In my java code, where I am using findVIewById , "ImageView" is shown red with a warning "cannot resolve symbol 'ImageView'". Do I need to import something??
What am I doing wrong?

Holly Fox
2,808 PointsOk, so I've worked out that you do indeed need to import something! Yay, that bit is now working!
I have set up a class to generate the random number and retrieve an image from an array.
How do I reference images from the array? I tried FindViewById but that doesn't seem to work.
Last question I promise! Thanks for your help :)

Gunjeet Hattar
14,483 PointsYou may ask as many as would like. Just what forum is for? In fact let me try and put my idea into action. That way it may answer most of your question. Let me get back in some time

Holly Fox
2,808 PointsA day at work interrupted my learning so sorry for the delay in responding!
Thank you for this, this helped me so much! Really appreciate you spending the time to put this together for me.
I'm just working through it and dropping elements into my own code. :)

Gunjeet Hattar
14,483 PointsOh no worries. Glad it helped. Shall look forward to see the finished version.
Michael Partridge
7,129 PointsMichael Partridge
7,129 PointsThis is extremely helpful! Thank you for taking the time to write this code. Two questions though. Is there any reason you initialized the variables and made them private at the top, or is that just personal preference/habit? Also is there a specific image type that Android Studio takes or does it accept JPG and PNG alike?