use*_*166 2 php mysql android image
我有一个 android 应用程序,它向服务器请求图像,并且应该在ImageView. 图像存储在图像文件夹中,其路径存储在MySQL数据库中。我php用于服务器端脚本。Android 应用程序请求将特定图像发送到image.php文件,该文件从数据库中获取存在该请求图像的文件夹的路径,并以 json 格式返回它。现在我的问题是:
仅该路径就足以在 android 应用程序中显示图像吗?还是应用程序应该先下载该图像以显示它?
我在某处看到,您可以将图像编码为字符串并将该字符串返回到 android 应用程序并将该字符串解码回图像?这是一种有效的方法吗?
这更像是一个普遍的问题,因此任何事情都会受到赞赏。
ByteArray使用BitmapFactory来获取Bitmap你的图像。或者使用像毕加索这样的库来“缓存”你的图像,就像在另一个答案中建议的那样。这个问题讨论了许多关于如何使用一些库在 Android 上下载图像的方法。或者您可以尝试使用本机方法,如下所示:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码取自此处。
String在您的服务器上将您的图像编码为 Base64 ,然后在您的 Android 上对其进行解码。要解码它,请尝试以下操作:
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.
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看该Bitmap课程的文档。
但老实说,您只是在该过程中添加了另一个步骤,并因此浪费了处理器周期。我想直接下载图像会更有效。
有关Base64该类的更多信息,请参阅文档。