How to make universal image loader load image buttons? - java

I'am sorry for the dumb question but I'am still a beginner.
I'am using universal image loader : https://github.com/nostra13/Android-Universal-Image-Loader
(Android, Eclipse)
and I don't know how to make it load my image buttons(nearly 30 imagebuttons per layout), I don't want to change anything in the image I just want it to display it as it is in the xml file.
So if you could tell me what code should I type that would be great, Thanks
for example if I have this ImageButton set up in my java file:
ImageButton staff = (ImageButton) findViewById(R.id.staff);
and my java file is called MainActivity and this image is saved in my drawable folder what should I do.
a part of my code:
public class MainActivity extends Activity implements OnClickListener {
SoundPool sp;
int soundId;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundId = sp.load(kh2hd.this, R.raw.btnclick, 1);
Picasso.with(this).load(R.drawable.staff).into(staff);
ImageButton staff = (ImageButton) findViewById(R.id.staffbtn);
staff.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.staffbtn:
sp.play(soundId, 1, 1, 0, 0, 1);
startActivity(new Intent("android.intent.action.INFO"));
break;

Download Picasso library from here
Add it to your build path. Import this library to your code file.
For loading from web url:
String url = "http://example.com"
Picasso.with(getApplicationContext()).load(url).into(imageView);
For loading from drawable:
Picasso.with(getApplicationContext()).load(R.drawable.imageFile).into(imageView);
Here, imageView is the name of variable you are using to represent ImageView. Eg.
ImageView imageView = (ImageView) findViewById(R.id.ImageView1);
If you are using ImageButton, change imageView variable to imageButton variable
ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton);
Note that I am passing variable name.

#user3896367 i think below code is use full for you. if you upload image from drawable of web url using this code.
download universal image loader jar.
put jar in lib folder and add jar.
create application class.
public static void initImageLoader(Context context) {
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(
new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.build();
ImageLoader.getInstance().init(config);
}
4.user imageloader like below syntext.
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageOnLoading(
R.drawable.wallpaper_placeholder)
.showImageForEmptyUri(
R.drawable.wallpaper_placeholder)
.showImageOnFail(R.drawable.wallpaper_placeholder)
.displayer(new FadeInBitmapDisplayer(500))
.cacheInMemory(true).cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();.
imageLoader.displayImage(
URL or ResourceID,
imageButton, options);
try this.

Related

How to set Image from Uri with a file:// scheme

I managed to use myimageview.setImageUri when the scheme was content://
But it doesnt work if my Uri has a file:// scheme. What is wrong and how can I make it work?
I have given both read and write permission.
Edit:
To make sure I haven't messed up anything, I created a whole new project.
Mainactivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
ArrayList<Uri> myimageUri = null;
myimageUri = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
Uri theuri = myimageUri.get(0);
ImageView myimageView = findViewById(R.id.imageView);
myimageView.setImageURI(theuri);
}
Attaching a debugger I found out that theuri is
file:///storage/emulated/0/WhatsApp/Media/WhatsApp%20Images/IMG-20171101-WA0005.jpg
It still doesn't work, but content :/// URIs work.
Try following code:
InputStream in = myimageView.g etContext().getContentResolver(). openInputStream(theuri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
in.close();
myimageView.setImageBitmap(bitmap);
You need to decode image file to a bitmap and give it to ImageView.
Bitmap mBitmap = Media.getBitmap(this.getContentResolver(), yourUri);
imageView.setImageBitmap(bitmap);

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'
....
}
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 (i.e. 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 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.
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.

Create ImageViews dynamically inside a loop

I have written this code that loads an image to in ImageView widget:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
i = (ImageView)findViewById(R.id.imageView1);
new get_image("https://www.google.com/images/srpr/logo4w.png") {
ImageView imageView1 = new ImageView(GalleryActivity.this);
ProgressDialog dialog = ProgressDialog.show(GalleryActivity.this, "", "Loading. Please wait...", true);
protected void onPreExecute(){
super.onPreExecute();
}
protected void onPostExecute(Boolean result) {
i.setImageBitmap(bitmap);
dialog.dismiss();
}
}.execute();
}
bu now, I want to load several images. for this I need create image views dynamically but i don't know how...
I want run my code inside a for loop:
for(int i;i<range;i++){
//LOAD SEVERAL IMAGES. READ URL FROM AN ARRAY
}
my main problem is creating several ImageViews inside a loop dynamically
you can modify the layout , image resource and no of images (may be dynamic as well) according to your requirement...
LinearLayout layout = (LinearLayout)findViewById(R.id.imageLayout);
for(int i=0;i<10;i++)
{
ImageView image = new ImageView(this);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
image.setMaxHeight(20);
image.setMaxWidth(20);
// Adds the view to the layout
layout.addView(image);
}
You can use this code to create ImageViews
ImageView image = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
image.setLayoutParams(vp);
image.setMaxHeight(50);
image.setMaxWidth(50);
// other image settings
image.setImageDrawable(drawable);
theLayout.addView(image);
where theLayout is the layout you want to add your image views to.
For more customization check out the dev page, where all the possible options are listed.
If your requirement is show in List or Gridview then you should go for Lazy Loading List or grid.
Please go through the below link.
https://github.com/thest1/LazyList
https://github.com/abscondment/lazy-gallery
Try this
rootLayout = (LinearLayout) view1.findViewById(R.id.linearLayout1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_VERTICAL;
params.setMargins(0, 0, 60, 0);
for(int x=0;x<2;x++) {
ImageView image = new ImageView(getActivity());
image.setBackgroundResource(R.drawable.ic_swimming);
rootLayout.addView(image);
}

load image into imageView from URL path in DB Table

I'm trying to follow this this tutorial to add image into imageView from URL.
public class AndroidLoadImageFromURLActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.image);
String image_url = "http://api.androidhive.info/images/sample.jpg";
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, image);
}
}
However, I have the image urls stored in my database table and would like to grab and display dynamically in java. please kindly show me how to do this in java? Thank you so much!
try this link
here there is a function named showImage() which accepts a url as parameter and returns bitmap which you can use to set to the image view.
and dont forget to give the internet permission when dealing with images to be displayed from a url

android imageview.setBackgroundResource() doesn't work

I have an imageview that should be changed on click
public class Settings extends Activity implements OnClickListener
{
private ImageView im1;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
im1 = (ImageView) findViewById( R.id.imageView1 );
im1.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (v == im1 )
{
Log.d("test", "hey!");
v.setBackgroundResource(R.drawable.img1);
}
}
}
when clicked the method runs and prints out "hey!" but the image won't change?
EDIT: forgot to mention that imageview contains another image provided by xml layout file
By convention, you should be using setImageResource(R.drawable.img1); (or setImageDrawable(getResources().getDrawable(R.drawable.img1));) instead of setBackgroundResource(R.drawable.img1);.
ImageView i = (ImageView) findViewById( R.id.imageView1 );
i.setImageResource(R.id.logo);
or
i.setBackgroundResource(R.drawable.icon);
UPDATE
Now, you can use like below,
imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.img1));
Try This In API 25
imgSchedule.setImageDrawable(getResources().getDrawable(R.drawable.circle_image_selected));
Its no convention that setimageResource() should be used.
Both the APIs can be used.
Also, in your case, it just looks like the case of out of sync resources.

Categories

Resources