Php 图像未从android上传到服务器
我是android新手,尝试将图像从android上传到服务器。 下面是代码。总是从php获取失败消息 imageupload.phpPhp 图像未从android上传到服务器,php,android,Php,Android,我是android新手,尝试将图像从android上传到服务器。 下面是代码。总是从php获取失败消息 imageupload.php $type1 = $_REQUEST['type'] ; $now = date("Y-m-d h:i:s"); if($type1 == "Exhibition"){ $type = 1; } if($type1 == "Wedding"){ $type = 2; } if($t
$type1 = $_REQUEST['type'] ;
$now = date("Y-m-d h:i:s");
if($type1 == "Exhibition"){
$type = 1;
}
if($type1 == "Wedding"){
$type = 2;
}
if($type1 == "Culture"){
$type = 3;
}
if($type1 == "Corporate"){
$type = 4;
}
if($type1 == "Sports"){
$type = 5;
}
if($type1 == "Others"){
$type = 6;
}
echo $img_file=$_FILES['image_file']['name'];
$img_ext=substr($img_file,-4);
$img_folder="include/img/";
$img_path=$img_folder.rand(10000,990000).$img_ext;
$img_type=pathinfo($img_file,PATHINFO_EXTENSION);
if((move_uploaded_file($_FILES["image_file"]["tmp_name"],$img_path)))
{
$jsonarray = array('result'=>'success','msg'=>'Event Added Successfully');
}else{
$jsonarray = array('result'=>'fail','msg'=>'Something went wrong');
echo $myJSON = json_encode($jsonarray);
}
}
else
{
$jsonarray = array('result'=>'fail','msg'=>'Failed To Add Event');
echo $myJSON = json_encode($jsonarray);
}
UpcomingEvents.java
public class UpcomingEvents extends AppCompatActivity {
Spinner spinner;
String type;
String[] SPINNERVALUES = {"Exhibition","Wedding","Culture","Corporate","Sports","Others"};
ImageView image;
Button choose, upload;
int PICK_IMAGE_REQUEST = 111;
String URL ="http://10.0.2.2/productioneventapi/imageupload.php";
Bitmap bitmap;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upcoming_events);
spinner =(Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpcomingEvents.this, android.R.layout.simple_list_item_1, SPINNERVALUES);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
type = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
image = (ImageView)findViewById(R.id.image);
choose = (Button)findViewById(R.id.imageupload);
upload = (Button) findViewById(R.id.addevent);
choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE_REQUEST);
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = new ProgressDialog(UpcomingEvents.this);
progressDialog.setMessage("Uploading, please wait...");
progressDialog.show();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
final String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){
@Override
public void onResponse(String s) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
String code = jsonObject.getString("msg");
if (code.equals("success")) {
Toast.makeText(UpcomingEvents.this,jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
}else {
Toast.makeText(UpcomingEvents.this,jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
}
}catch (JSONException e) {
e.printStackTrace();
Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+s);
}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(UpcomingEvents.this, "Some error occurred -> "+volleyError, Toast.LENGTH_LONG).show();;
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("image_file", imageString);
parameters.put("type",type);
return parameters;
}
};
RequestQueue rQueue = Volley.newRequestQueue(UpcomingEvents.this);
rQueue.add(request);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
image.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
公共类UpcomingEvents扩展AppCompative活动{
纺纱机;
字符串类型;
String[]SPINNERVALUES={“展览”、“婚礼”、“文化”、“企业”、“体育”、“其他”};
图像视图图像;
按钮选择,上传;
int PICK_IMAGE_REQUEST=111;
字符串URL=”http://10.0.2.2/productioneventapi/imageupload.php";
位图;
进行对话进行对话;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u即将到来的事件);
微调器=(微调器)findViewById(R.id.spinner);
ArrayAdapter=新的ArrayAdapter(UpcomingEvents.this,android.R.layout.simple\u list\u item\u 1,SPINNERVALUES);
旋转器。设置适配器(适配器);
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图、,
内部位置,长id){
type=parent.getItemAtPosition(position.toString();
}
@凌驾
未选择公共无效(AdapterView父级){
//TODO自动生成的方法存根
}
});
image=(ImageView)findviewbyd(R.id.image);
选择=(按钮)findViewById(R.id.imageupload);
上传=(按钮)findViewById(R.id.addevent);
选择.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意图=新意图();
intent.setType(“image/*”);
intent.setAction(intent.ACTION\u PICK);
startActivityForResult(Intent.createChooser(Intent,“选择图像”),PICK\u Image\u请求);
}
});
upload.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
progressDialog=新建progressDialog(UpcomingEvents.this);
progressDialog.setMessage(“正在上载,请稍候…”);
progressDialog.show();
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
压缩(bitmap.CompressFormat.JPEG,100,baos);
byte[]imageBytes=bas.toByteArray();
最终字符串imageString=Base64.encodeToString(imageBytes,Base64.DEFAULT);
StringRequest=newStringRequest(request.Method.POST,URL,new Response.Listener()){
@凌驾
公共void onResponse(字符串s){
progressDialog.disclose();
试一试{
JSONObject JSONObject=新的JSONObject;
字符串代码=jsonObject.getString(“msg”);
如果(代码等于(“成功”)){
Toast.makeText(UpcomingEvents.this,jsonObject.getString(“msg”),Toast.LENGTH\u LONG.show();
}否则{
Toast.makeText(UpcomingEvents.this,jsonObject.getString(“msg”),Toast.LENGTH\u LONG.show();
}
}捕获(JSONException e){
e、 printStackTrace();
Log.e(“JSON解析器”,“错误解析数据[”+e.getMessage()+“]””+s);
}
}
},new Response.ErrorListener(){
@凌驾
公共错误响应(截击错误截击错误){
Toast.makeText(UpcomingEvents.this,“发生了一些错误->”+截击错误,Toast.LENGTH_LONG.show();;
}
}) {
@凌驾
受保护的映射getParams()引发AuthFailureError{
映射参数=新的HashMap();
参数put(“图像文件”,imageString);
参数。输入(“类型”,类型);
返回参数;
}
};
RequestQueue rQueue=Volley.newRequestQueue(UpcomingEvents.this);
rQueue.add(请求);
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==PICK\u IMAGE\u REQUEST&&resultCode==RESULT\u OK&&data!=null&&data.getData()!=null){
Uri filePath=data.getData();
试一试{
位图=MediaStore.Images.Media.getBitmap(getContentResolver(),filePath);
setImageBitmap(位图);
}捕获(例外e){
e、 printStackTrace();
}
}
}
}
在android中,图像编码为base64,如何在php中解码?
我试着用
$imageData=base64_解码($_请求['image_文件]);
$img_file=$imageData强>
这也不管用,,
我的方式是否错误?
提前谢谢
parameters.put("image_file", imageString);
如果这样做,则发送参数中编码的文件base64
php脚本希望您发送一个文件。如果保留截取代码,则可以使用
$imageString = $_REQUEST['image_file'];
您的move\u上传的文件()
现在无法使用
但是
$imageData=base64_解码($_请求['image_文件])代码>应该可以工作。您只需自己将数据保存到文件中即可。'msg'=>“出错了”)代码>这不是很有用。你应该改变脚本,让它说出真正的原因。好的。但它也给出了同样的消息“添加事件失败”,这也是一个非常糟糕的错误消息。因为它没有说明原因。$img_file=$imageData代码>我们想知道你对这句话有什么想法。我使用了“file\u put\u contents”(“include/img/”$img\u path,$imageData);”那么也不起作用了,对吗?如果路径是正确的,那么应该这样做。返回值是多少?您得到的错误是什么?那个目录存在吗?你可以先检查存在,然后再把东西放进去。请告诉“include/img/”$img_path
的值。如果失败,则让php脚本回显此路径。提供信息。