Hello I am new with Google Glass and am wondering if I can display an image from the web as my background image.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String address = "http://archiveteam.org/images/1/15/Apple-logo.jpg";
Card myCard = new Card(this);
myCard.setText("Hello, World!");
myCard.setFootnote("First Glassware for Glass");
myCard.setImageLayout(Card.ImageLayout.FULL);
myCard.addImage(new URL(address));
View cardView = myCard.getView();
// Display the card we just created
setContentView(cardView);
}
I saw in a few threads that myCard.addImage(new URL(address)) was the solution, but I am getting the following error on that line.
The method addImage(Drawable) in the type Card is not applicable for the arguments (URL)
Any help would be appreciated, thanks in advance!
You shouldn't be gathering images on the UI thread. That is a network operation.
Create a separate Method running on a new thread to download the image, then save it as a bitmap. Then save that to the card;
DownloadImageTask(myCard).execute(address);
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
Card imgCard;
public DownloadImageTask(Card imgCard) {
this.imgCard= imgCard;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
imgCard.setImage(result);
}
}
Related
I'm trying to download an image using a URL and a button in my app. When I'm running it on my phone, I,m not able to download the image. Can anyone please point out the problem with this. Thanks for the help in advance :)
This is my code.
public class MainActivity extends AppCompatActivity {
ImageView download;
public void downloadImage(View view){
DownloadImage image = new DownloadImage();
Bitmap result;
try {
result = image.execute("https://en.wikipedia.org/wiki/File:Bart_Simpson_200px.png").get();
download.setImageBitmap(result);
}
catch(Exception e)
{
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
download = (ImageView) findViewById(R.id.imageView);
}
public class DownloadImage extends AsyncTask<String, Void, Bitmap>{
#Override
protected Bitmap doInBackground(String... urls) {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.connect();
InputStream in = urlConnection.getInputStream();
Bitmap Image = BitmapFactory.decodeStream(in);
in.close();
return Image;
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
you can download image from url in two ways
1.you can user Glide library to load image from url look the below code it can help you in simple way
compile this library
implementation 'com.github.bumptech.glide:glide:4.6.1'
than load image like this
Glide.with(MainActivity.this)
.load(url)
.apply(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
.into(imageview);
2 .try this if you dont want to use third party library
new DownloadImage(imamgeview).execute(url);
create a Async Task
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImage(ImageView bmImage) {
this.bmImage = (ImageView ) bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.d("Error", e.getStackTrace().toString());
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
i hope that it will work in your case
You did not call the function
downloadImage(view);
In the oncreate (after the findviewbyid line)
Also check if the internet permision is in the Android Manifest file :
<uses-permission android:name="android.permission.INTERNET" />
Having said that you should use a library like Glide\Picasso\Ion etc... much better than asynctask for this purpuse
https://github.com/bumptech/glide - Glide
https://github.com/koush/ion - Ion
http://square.github.io/picasso/ - picasso
Hope it helped.
I want to read image from server using android studio via emulator. Please any one help me because I had tried a lot of ways but not yet success.
My peace of code are like this.
In Acitivity.java file code is like this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.button);
imageView=(ImageView)findViewById(R.id.imageView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
iv = (ImageView) findViewById(R.id.imageView);
bitmap = getBitmapFromURL("http://10.0.2.2/img.bmp");
iv.setImageBitmap(bitmap);
}
});
}
And I use this Function.
public Bitmap getBitmapFromURL(String src){
try{
URL url= new URL(src);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input=(connection).getInputStream();
Bitmap myBitmap= BitmapFactory.decodeStream(input);
return myBitmap;
}catch (Exception e){
e.printStackTrace();
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
return null;
}
}
Please any one help me.
Use Picasso for downloading and caching images.Add this in your dependencies using compile 'com.squareup.picasso:picasso:2.5.2' if you are using android studio otherwise add the jar in your libs.
it's a one line code to manage everything :
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
just pass your url and you are done.
I try many things at last I found this I think this will useful for you. So try this.
Do this in your Activity.
new DownloadImageTask(your_imageview).execute("http://10.0.2.2/img.bmp);
And method is like this.
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
bmImage.setImageBitmap(result);
}
}
how to get tweet post with image and display it on list view?
i'm only getting text in retrieving tweet post on twitter.
this is what i wanted to do http://tinypic.com/r/2ztee84/8
and this is what I've done on my application http://tinypic.com/r/zkmknm/8
thanks in advance.
Well, it would help if you provided us with more code. But... Try to "parse" the image link from the tweet, and download it.
You could try this lib, which will download images asynchronously. Or you could try a native approach. Something like:
public class LoginActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
this.findViewById(R.id.userinfo_submit).setOnClickListener(this);
// Verify Code
LinearLayout view = (LinearLayout) findViewById(R.id.txt_verify_code);
view.addView(new VerifyCodeView(this));
// show The Image
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute(“http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png”);
}
public void onClick(View v) {
startActivity(new Intent(this, IndexActivity.class));
finish();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String… urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e(“Error”, e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
This code was taken from here.
And there's this related question, which might be an interesting read for you.
I have an image view in my Android app, where I have to set a simple image from url. I tried the below code, but it doesn't set the image from url.
try {
URL url = new URL("https://drive.google.com/...");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream stream = connection.getInputStream();
Bitmap teamBmpImage = BitmapFactory.decodeStream(stream);
teamImgView.setImageBitmap(teamBmpImage);
}
catch (Exception e) {
}
Could someone guide me to achieve this please?
UPDATED CODE: Which gives Nullpointer exception
public class AboutActivity extends ActionBarActivity {
ImageView teamImgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
teamImgView = (ImageView) this.findViewById(R.id.teamImageView);
new DownloadImageTask(teamImgView).execute("http://docs.oracle.com/javase/tutorial/2d/images/examples/strawberry.jpg");
}
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
//pd.show();
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
//pd.dismiss();
bmImage.setImageBitmap(result);
}
}
}
I guess you are executing your code on the MainThread, which leads to a NetworkOnMainThreadException in android. Try to execute your code asynchronous like in the example below
new AsyncTask<String, Integer, Bitmap>() {
#Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Bitmap bm) {
ImageView teamImgView = (ImageView) findViewById(R.id.teamImageView);
teamImgView.setImageBitmap(bm);
}
}.execute("https://drive.google.com/uc?....");
You can use Picasso library and here is a detailed tutorial on how to do this.
This is very simple example usage
Picasso.with(activityContext)
.load("https://drive.google.com/uc?....")
.placeholder(R.drawable.image_name)
.into(imageView);
As bojan says you can use Picasso library wich handles many common pitfalls of image loading on Android.
Picasso.with(context).load("http://myurl/myImage.png").into(imageView);
Picasso
Anyway, check out this threat too :)
How to load an ImageView by URL in Android?
Try following this link:
http://www.tutorialsbuzz.com/2014/11/android-volley-url-imageview.html
This will help you to load your image using Volley library which will do all the networking stuff on networking thread and set your image on main UI thread. It has also the LRUCache part which you can skip if you want.
I have an AsyncTask that gets favicon base on a URL.
I am creating a listView, that has a favicon and a URL. at the moment even though I have the AsyncTask my UI waits for the asyncTask to finish before it shows the next activity.
I would like my activity to start with a default image that is stored in drawable, and that the AsyncTask will replace the images after it got each favicon.
Any ideas how to do it?
my AsyncTask:
private class DownloadImageTask extends AsyncTask<URL, Void, Bitmap> {
final AccountListModel model = new AccountListModel(daoSession);
protected Bitmap doInBackground(URL... urls) {
URL urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
mIcon11 = model.getBitmapFromURL(urldisplay);
//try to get image
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
faviconBitmap=result;
}}
the way I am currently creating my list view
for (Site site : model.getSites()) {
try {
String url = site.getDomain()
faviconBitmap= new DownloadImageTask().execute(new URL("http", "www."+ url.trim(),"/favicon.ico")).get();
if (faviconBitmap != null) {
Bitmap scaled = Bitmap.createScaledBitmap(faviconBitmap,32, 32, true);
accountsAndUsersList.add(newAccountListScreen(scaled,site.getName());
}
}
else {
Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.favicon);
Bitmap scaled = Bitmap.createScaledBitmap(icon, 32, 32, true);
accountsAndUsersList.add(new AccountListScreen(scaled,site.getName());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sitesToSortArray = new AccountListScreen[accountsAndUsersList
.size()];
accountsAndUsersList.toArray(sitesToSortArray);
AccountListAdapter adapter = new AccountListAdapter(this,
R.layout.account_list_row, sitesToSortArray);
listViewAccountList = (ListView) findViewById(R.id.activityAccountList);
listViewAccountList.setAdapter(adapter);
please let me know if you would like to see my adapter as well.
Thank you!