I have an android application which request for images to server and should display that request image in ImageView
. Images are stored in image folder and its path is stored in MySQL
database. I am using php
for server side scripting. Android app request for particular image to image.php
file which brings the path of the folder in which that requested image is present from database and returns it in json format.
Now my question is:
Is that path alone enough for displaying image in android app? Or should the app download that image first in order to display it?
I saw somewhere in SO that you can encode image to string and return that string to android app and decode that string back to image? Is it an efficient way?
It is more of a general question so any thing would be appreciated.
答案 0 :(得分:3)
ByteArray
using BitmapFactory
to get the Bitmap
of your image. Or use a lib like Picasso to "cache" your image, like it was suggested on another answer.This question discusses many methods on how to download images on Android using some libs. Or you could try a native approach, like this:
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.
String
on your server and decode it on your Android later.To decode it try the following:
byte[] data = Base64.decode(base64Image, Base64.DEFAULT);
Bitmap bm;
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
bm = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
// Now do whatever you want with the Bitmap.
You can see the docs for the Bitmap
class here.
But honestly, you're just adding another step to the process and wasting processor cycles with that. I guess it's more efficient to just download the image as it is.
For more info on the Base64
class, please refer to the docs.
答案 1 :(得分:2)
Try Picasso. It's easy and simple
Picasso.with(context)
.load("http://i.imgur.com/DvpvklR.png")//Your image link url
.into(imageView);//Your imageView
Full docs can be found here.
答案 2 :(得分:2)
If you have a URL for the image, I would recommend using Glide or Picasso.
GLIDE:
https://github.com/bumptech/glide
Glide.with(myFragment)
.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.into(myImageView);
PICASSO:
http://square.github.io/picasso/
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
If you are using Android Studio, either of these libraries can easily be added to your Android project by adding them as dependencies in your app's Gradle file.
Add this to dependencies in your Gradle file for Glide:
compile 'com.github.bumptech.glide:glide:3.6.0'
Add this to dependencies in your Gradle file for Picasso:
compile 'com.squareup.picasso:picasso:2.5.2'