简体   繁体   中英

Android Set ImageView Source from Java

I want to set an Image Source in Android,

XML :

 <ImageView
    android:layout_width="200dp"
    android:layout_height="300dp"
    android:id="@+id/main"
    android:src="@drawable/malayali"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="80dp"
    />

Java :

public class MainActivity extends ActionBarActivity {

  public SharedPreferences exactPreferences;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    exactPreferences = getSharedPreferences("details",MODE_PRIVATE);

    getSupportActionBar().hide();

    new RetriveIcon().execute();    //Api Calling & Storing value in SharedPreference

    String value = exactPreferences.getString("main_url",null);

    Log.i("from exactpreference",value);  // Working fine !!!  (http://www.exampple.com/storage/images/filename.jpeg)

   ImageView banner = (ImageView)findViewById(R.id.main);
   banner.setImageDrawable(getResources().getDrawable(R.drawable.value));

    setContentView(R.layout.activity_main);

 }
}

androidStudio showing error in the below line of value as red.

banner.setImageDrawable(getResources().getDrawable(R.drawable.value));

ERROR:

Cannot resolve symbol 'value'

How can I solve this Error ?

If you are dealing with the drawable name in your Resources folder, then Your problem is one of those options:-

1)You don't have the drawable value in your Resources. Make sure it exists in your resources folder.

2)You are importing the wrong R in your activity Make sure you are importing your application R not the android one.

If your are using a Direct URL for the image. I recommend you to use Universal Lazy Loader third party for that. All you have to do is to pass the image direct URL and the ImageView and it will do the job for you.

In your Gradle file "Module:app"

dependencies {
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
    ....
 }

Try this Simple code

 ImageView banner = (ImageView)findViewById(R.id.main);
 banner.setBackgroundResource(R.drawable.value);

But image name must be value and store in your drawable folder.

Are you trying to load an image from your drawable folder or download one from the Internet? This:

banner.setImageDrawable(getResources().getDrawable(R.drawable.value));

Will work if you have a value.png file in one of your drawable folders. If you are trying to load an image from the net (ie something like this: http://www.exampple.com/storage/images/filename.jpeg ), you'll need to download it first and set the bitmap as an ImageView source. There are many 3rd party libraries doing this. Take a look at Picasso :

Picasso.with(this).load(value).into(banner);

EDIT: In order to add Picasso you need to update the build.gradle file (the one which refers to a module not project). You should have a section like this:

dependencies {
...
}

You need to add this line inside it:

compile 'com.squareup.picasso:picasso:2.5.2'

Try this :

String uri = "@drawable/myresource.png";

int imageResource = getResources().getIdentifier(uri, null, getPackageName());

imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);

Note : This is sample code.

You want to set your imageview's background with an online image file. You can use this example on your project. Usage of this is very easy.

Also in your codes, you should change some lines after adding classes from the above link:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    exactPreferences = getSharedPreferences("details",MODE_PRIVATE);

    getSupportActionBar().hide();

    new RetriveIcon().execute();    //Api Calling & Storing value in SharedPreference

    String value = exactPreferences.getString("main_url",null);

    Log.i("from exactpreference",value);  // Working fine !!!  (http://www.exampple.com/storage/images/filename.jpeg)

    // ImageLoader class instance
    ImageLoader imgLoader = new ImageLoader(getApplicationContext());

    ImageView banner = (ImageView)findViewById(R.id.main);
    imgLoader.DisplayImage(value, 0, banner);
}

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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