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
Related
I'm trying to load the images in an image slider from firestore. The image urls are there in firestore. I'm using the following library https://github.com/denzcoskun/ImageSlideshow for image slider, but it shows error instead of images.
Image of the output
Logcat Image
Image of firestore structure
This is the java code:
public class preview_product extends AppCompatActivity
{
private String position;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private ImageSlider imageSlider;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_product_preview);
imageSlider = findViewById(R.id.image_slider);
final List<SlideModel> displayImages = new ArrayList<>();
Bundle intent = getIntent().getExtras();
position = intent.get("key").toString();
db.collection("Store").document("name").collection("prod").document(position).collection("details").document("product_id").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>()
{
#Override
public void onSuccess(#NonNull DocumentSnapshot documentSnapshot)
{
List<String> Urls = (List<String>) documentSnapshot.get("Images");
displayImages.add(new SlideModel(Urls.toString(), ScaleTypes.FIT));
imageSlider.setImageList(displayImages,ScaleTypes.FIT);
imageSlider.startSliding(3000);
}
});
}
}
I guess you've copied the link of the webpage-showing the image ,instead of the link of actual image.
Try opening the image in a new tab, and then copy the link from the address-bar.
Maybe, this is what you've copied !
This is what you should try coping instead !
Hope this helps!
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);
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.
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.
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
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 :)