如何将图像上传到特定的URL并在android中获取到imageView?
我正在开发一个android应用程序,我需要上传一个图像到下面的URL 当我上传图像到这个URL时,图像应该存储在 用户可以在他/她想从这个url看到它的时候看到它 拜托,我不知道该怎么办。请帮助我或给我发送任何链接 提前谢谢 遵循给定的步骤: 1) 您必须将图像转换为位图 2) 将位图转换为Base64字符串 3) 将此字符串发送到服务器 要表示此图像,请执行以下操作: 1) 将此Base64转换为位图,并将此位图设置为imageview 遵循给定的链接 按照给定步骤操作: 1) 您必须将图像转换为位图 2) 将位图转换为Base64字符串 3) 将此字符串发送到服务器 要表示此图像,请执行以下操作: 1) 将此Base64转换为位图,并将此位图设置为imageview 遵循给定的链接 试试这个(使用截击上传图像) 我只是从android端和php文件中编写upload方法的片段来接收文件(以字符串的形式) ANDROID/JAVA代码如何将图像上传到特定的URL并在android中获取到imageView?,android,web-services,url,upload,imageview,Android,Web Services,Url,Upload,Imageview,我正在开发一个android应用程序,我需要上传一个图像到下面的URL 当我上传图像到这个URL时,图像应该存储在 用户可以在他/她想从这个url看到它的时候看到它 拜托,我不知道该怎么办。请帮助我或给我发送任何链接 提前谢谢 遵循给定的步骤: 1) 您必须将图像转换为位图 2) 将位图转换为Base64字符串 3) 将此字符串发送到服务器 要表示此图像,请执行以下操作: 1) 将此Base64转换为位图,并将此位图设置为imageview 遵循给定的链接 按照给定步骤操作: 1) 您必须将
// getStringImage method will be called inside uploadImage
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
private void uploadImage(){
//Showing the progress dialog
final ProgressDialog loading = ProgressDialog.show(this, "Uploading...", "Please wait...", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//Disimissing the progress dialog
loading.dismiss();
//Showing toast message of the response
Toast.makeText(MainActivity.this, "uploaded" , Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
//Showing toast
Toast.makeText(MainActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(bitmap);
//Getting Image Name
String name = editTextName.getText().toString().trim();
//Creating parameters
Map<String,String> params = new Hashtable<String, String>();
//Adding parameters
params.put(KEY_IMAGE, image);
params.put(KEY_NAME, "name");
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
//将在uploadImage内调用getStringImage方法
公共字符串getStringImage(位图bmp){
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[]imageBytes=bas.toByteArray();
字符串encodedImage=Base64.encodeToString(imageBytes,Base64.DEFAULT);
返回图像;
}
私有void uploadImage(){
//显示进度对话框
final ProgressDialog loading=ProgressDialog.show(这是“正在上载…”,“请稍候…”,false,false);
StringRequest StringRequest=新建StringRequest(Request.Method.POST,UPLOAD\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串s){
//取消进度对话框的限制
loading.dispose();
//显示响应的toast消息
Toast.makeText(MainActivity.this,“上载”,Toast.LENGTH_LONG.show();
}
},
新的Response.ErrorListener(){
@凌驾
公共错误响应(截击错误截击错误){
//取消“进度”对话框
loading.dispose();
//敬酒
Toast.makeText(MainActivity.this,volleyError.getMessage().toString(),Toast.LENGTH_LONG.show();
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
//将位图转换为字符串
字符串图像=getStringImage(位图);
//获取图像名称
字符串名称=editTextName.getText().toString().trim();
//创建参数
Map params=新哈希表();
//添加参数
参数put(按键图像,图像);
参数put(键名称,“名称”);
//返回参数
返回参数;
}
};
//创建请求队列
RequestQueue RequestQueue=Volley.newRequestQueue(this);
//将请求添加到队列
添加(stringRequest);
}
PHP端img.PHP
<?php
$host="localhost"; //replace with database hostname
$username="root"; //replace with database username
$password=""; //replace with database password
$db_name="mydb"; //replace with database name
$con=mysql_connect($host,$username,$password);
$db=mysql_select_db($db_name);
$name = $_REQUEST['name']; //image name
$image = $_REQUEST['image']; //image in string format
$user=$_REQUEST['User_ID'];
$decodedImage = base64_decode($image);
$image_file=time().rand(1111,9999);
$name=$name.$image_file;
$base_path='/var/www/html/uploads/';
file_put_contents($base_path.$name.".jpg", $decodedImage);
mysql_query("INSERT into `image`(`img`,`User_ID`,`date`) values ('".$image_file.".jpg','$user',now() )");
echo mysql_insert_id();
?>
试试这个(使用截击上传图像)
我只是从android端和php文件中编写upload方法的片段来接收文件(以字符串的形式)
ANDROID/JAVA代码
// getStringImage method will be called inside uploadImage
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
private void uploadImage(){
//Showing the progress dialog
final ProgressDialog loading = ProgressDialog.show(this, "Uploading...", "Please wait...", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//Disimissing the progress dialog
loading.dismiss();
//Showing toast message of the response
Toast.makeText(MainActivity.this, "uploaded" , Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
//Showing toast
Toast.makeText(MainActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(bitmap);
//Getting Image Name
String name = editTextName.getText().toString().trim();
//Creating parameters
Map<String,String> params = new Hashtable<String, String>();
//Adding parameters
params.put(KEY_IMAGE, image);
params.put(KEY_NAME, "name");
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
//将在uploadImage内调用getStringImage方法
公共字符串getStringImage(位图bmp){
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[]imageBytes=bas.toByteArray();
字符串encodedImage=Base64.encodeToString(imageBytes,Base64.DEFAULT);
返回图像;
}
私有void uploadImage(){
//显示进度对话框
final ProgressDialog loading=ProgressDialog.show(这是“正在上载…”,“请稍候…”,false,false);
StringRequest StringRequest=新建StringRequest(Request.Method.POST,UPLOAD\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串s){
//取消进度对话框的限制
loading.dispose();
//显示响应的toast消息
Toast.makeText(MainActivity.this,“上载”,Toast.LENGTH_LONG.show();
}
},
新的Response.ErrorListener(){
@凌驾
公共错误响应(截击错误截击错误){
//取消“进度”对话框
loading.dispose();
//敬酒
Toast.makeText(MainActivity.this,volleyError.getMessage().toString(),Toast.LENGTH_LONG.show();
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
//将位图转换为字符串
字符串图像=getStringImage(位图);
//获取图像名称
字符串名称=editTextName.getText().toString().trim();
//创建参数
Map params=新哈希表();
//添加参数
参数put(按键图像,图像);
参数put(键名称,“名称”);
//返回参数
返回参数;
}
};
//创建请求队列
RequestQueue RequestQueue=Volley.newRequestQueue(this);
//将请求添加到队列
添加(stringRequest);
}
PHP端img.PHP
<?php
$host="localhost"; //replace with database hostname
$username="root"; //replace with database username
$password=""; //replace with database password
$db_name="mydb"; //replace with database name
$con=mysql_connect($host,$username,$password);
$db=mysql_select_db($db_name);
$name = $_REQUEST['name']; //image name
$image = $_REQUEST['image']; //image in string format
$user=$_REQUEST['User_ID'];
$decodedImage = base64_decode($image);
$image_file=time().rand(1111,9999);
$name=$name.$image_file;
$base_path='/var/www/html/uploads/';
file_put_contents($base_path.$name.".jpg", $decodedImage);
mysql_query("INSERT into `image`(`img`,`User_ID`,`date`) values ('".$image_file.".jpg','$user',now() )");
echo mysql_insert_id();
?>
查看上传图像时我的回答错误。您需要配置服务器a