Android应用程序发送图像到MySQL [英] Android application to send image to MySQL
本文介绍了Android应用程序发送图像到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有它发送一个图像到我的数据库作为一个Android应用;
公共类NewProductActivity延伸活动{
//进度对话框
私人ProgressDialog pDialog;
公共字符串image_str;
JSONParser jsonParser =新JSONParser();
的EditText inputName;
的EditText inputPrice;
的EditText inputDesc;
的EditText inputImg;
按钮btnTakePhoto;
ImageView的imgTakenPhoto;
私有静态最终诠释CAM_REQUREST = 1313;
// URL,创建新产品
私有静态字符串url_create_product =http://buiud.com/android_connect/create_product.php;
// JSON节点名称
私有静态最后弦乐TAG_SUCCESS =成功;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.add_product);
//编辑文本
inputName =(EditText上)findViewById(R.id.inputName);
inputPrice =(EditText上)findViewById(R.id.inputPrice);
inputDesc =(EditText上)findViewById(R.id.inputDesc);
// inputImg =(EditText上)findViewById(R.id.imageView1);
点阵位图= BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream流=新ByteArrayOutputStream();
bitmap.com preSS(Bitmap.Com pressFormat.PNG,90,流); // COM preSS到你想要的格式。
byte []的byte_arr = stream.toByteArray();
image_str = Base64.en codeToString(byte_arr,Base64.DEFAULT);
//创建按钮
按钮btnCreateProduct =(按钮)findViewById(R.id.btnCreateProduct);
//按钮点击事件
btnCreateProduct.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图查看){
//创建在后台线程的新产品
新CreateNewProduct()执行()。
}
});
btnTakePhoto =(按钮)findViewById(R.id.button1);
imgTakenPhoto =(ImageView的)findViewById(R.id.imageView1);
btnTakePhoto.setOnClickListener(新btnTakePhotoClicker());
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
// TODO自动生成方法存根
super.onActivityResult(要求code,因此code,数据);
如果(要求code == CAM_REQUREST){
。位图的缩略图=(位图)data.getExtras()获得(数据);
imgTakenPhoto.setImageBitmap(缩略图);
}
}
类btnTakePhotoClicker实现Button.OnClickListener
{
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAM_REQUREST);
}
}
/ **
*背景异步任务来创建新产品
* * /
类CreateNewProduct扩展的AsyncTask<字符串,字符串,字符串> {
/ **
*在启动后台线程显示进度对话框
* * /
@覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(NewProductActivity.this);
pDialog.setMessage(创建产品..);
pDialog.setIndeterminate(假);
pDialog.setCancelable(真正的);
pDialog.show();
}
/ **
*创建产品
* * /
保护字符串doInBackground(字符串参数... args){
字符串名称= inputName.getText()的toString()。
串价格= inputPrice.getText()的toString()。
。字符串描述= inputDesc.getText()的toString();
//字符串image_str = inputImg.getText()的toString()。
//大厦参数
名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
params.add(新BasicNameValuePair(姓名,名字));
params.add(新BasicNameValuePair(价格,价格));
params.add(新BasicNameValuePair(说明,说明));
params.add(新BasicNameValuePair(IMG,image_str));
//params.add(new BasicNameValuePair(图像,图像));
//获取JSON对象
//注意,创造产品的URL接受POST方法
JSONObject的JSON = jsonParser.makeHtt prequest(url_create_product,
POST,则params);
//检查日志猫来回响应
Log.d(创建回应,json.toString());
//检查成功标签
尝试 {
INT成功= json.getInt(TAG_SUCCESS);
如果(成功== 1){
//创建成功产品
意图I =新的意图(getApplicationContext(),AllProductsActivity.class);
startActivity(ⅰ);
//关闭此屏幕
完();
} 其他 {
//创建失败产品
}
}赶上(JSONException E){
e.printStackTrace();
}
返回null;
}
/ **
*在完成后台任务之后关闭该进度对话框
* ** /
保护无效onPostExecute(字符串file_url){
//驳回一旦完成对话
pDialog.dismiss();
}
}
}
甚至认为,我希望它出现在数据库中的.bin,我怎样才能改变这种状况上的图像被.png格式,我看了一下打字的东西,当应用程序被调用create.php,但没有雪茄。我甚至无法显示的.bin作为HTML页面上的图像。
解决方案
< PHP
$ NAME = $ _ POST ['形象'];
$进入= base64_de code($名);
$图像= imagecreatefromstring($输入);
$目录=目录名(__ FILE __)DIRECTORY_SEPARATOR图像/DIRECTORY_SEPARATOR指数$标题为JPEG。。。。
标题(内容类型:image / JPEG');
$ imagetojpg = imagejpeg($形象,$目录);
imagedestroy($图像);
ReadFile的($目录);
出口 ();
?>
这应该工作fine.image是包含的base64 EN codeD字符串的变量。
I have an android application which sends an image to my database as;
public class NewProductActivity extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
public String image_str;
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputPrice;
EditText inputDesc;
EditText inputImg;
Button btnTakePhoto;
ImageView imgTakenPhoto;
private static final int CAM_REQUREST = 1313;
// url to create new product
private static String url_create_product = "http://buiud.com/android_connect/create_product.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_product);
// Edit Text
inputName = (EditText) findViewById(R.id.inputName);
inputPrice = (EditText) findViewById(R.id.inputPrice);
inputDesc = (EditText) findViewById(R.id.inputDesc);
//inputImg = (EditText) findViewById(R.id.imageView1);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte[] byte_arr = stream.toByteArray();
image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
// Create button
Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});
btnTakePhoto = (Button) findViewById(R.id.button1);
imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);
btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAM_REQUREST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imgTakenPhoto.setImageBitmap(thumbnail);
}
}
class btnTakePhotoClicker implements Button.OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAM_REQUREST);
}
}
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewProductActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = inputName.getText().toString();
String price = inputPrice.getText().toString();
String description = inputDesc.getText().toString();
//String image_str = inputImg.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", description));
params.add(new BasicNameValuePair("img",image_str));
//params.add(new BasicNameValuePair("image", image));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
Even thought I want the image to be .png it appears on the database as .bin, how can I change that, I have read about typing something when the application was calling the create.php but no cigar. I can't even display the .bin as an image on the HTML page.
解决方案
<?php
$name = $_POST['image'];
$entry = base64_decode($name);
$image = imagecreatefromstring($entry);
$directory = dirname(__FILE__).DIRECTORY_SEPARATOR."images/".DIRECTORY_SEPARATOR."index".$title.".jpeg";
header ( 'Content-type:image/jpeg' );
$imagetojpg=imagejpeg($image, $directory);
imagedestroy($image );
readfile ($directory);
exit ();
?>
This should work fine.image is a variable which contain base64 encoded string.
这篇关于Android应用程序发送图像到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文