映像未在服务器中生成/上载

时间:2018-12-26 11:23:08

标签: java php android web-services server

我正在开发一个Android应用,我想将图像保存在服务器(免费服务器000webhost)上。为此,我使用base64将图像编码为字符串。然后使用volley使用POST方法发送此字符串。 但是映像不是在服务器端生成的。谁能告诉我怎么了?

upload.php

<?php

$servername = "localhost";
$database = "id8258200_image";
$username = "id8258200_qmine1";
$password = "";


$conn = mysqli_connect($servername, $username, $password, $database);


if (!$conn) {
   die("Connection failed: " . mysqli_connect_error());
 }

echo "Connected successfully";

 if(isset($_POST["image"])){

$encoded_string = $_POST["image"];

$image_name="img";
$decoded_string = base64_decode($encoded_string);

$path = "image/$image_name.jpeg";

$file = fopen($path, 'wb');

$is_written = fwrite($file, $decoded_string);
fclose($file);
echo "success";
 }
 else {
      echo "error";
 }
?>

Mainactivity.java

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

public static final String UPLOAD_URL = "https://qmine.000webhostapp.com/question/upload.php";


private int PICK_IMAGE_REQUEST = 1;

private Button buttonChoose;
private Button buttonUpload;
private Button buttonView;

private ImageView imageView;

private Bitmap bitmap;

private Uri filePath;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonChoose = (Button) findViewById(R.id.buttonChoose);
    buttonUpload = (Button) findViewById(R.id.buttonUpload);
    buttonView = (Button) findViewById(R.id.buttonViewImage);

    imageView = (ImageView) findViewById(R.id.imageView);

    buttonChoose.setOnClickListener(this);
    buttonUpload.setOnClickListener(this);
    buttonView.setOnClickListener(this);
}

private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_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) {

        filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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);
    Log.d("tr", "qwerty" + encodedImage);
    return encodedImage;
}

private void uploadImage() {

    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    StringRequest request = new StringRequest(Request.Method.POST, UPLOAD_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), "errorrrr", Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            Map<String, String> Params = new HashMap<String, String>();
            String img = getStringImage(bitmap);
            Log.d("qw", "aasas" + img);
            Params.put("image", img);
            return Params;

        }

    };

    queue.add(request);

}

@Override
public void onClick(View v) {
    if (v == buttonChoose) {
        showFileChooser();
    }

    if (v == buttonUpload) {
        uploadImage();
    }

    if (v == buttonView) {
        viewImage();
    }
}

private void viewImage() {
    // startActivity(new Intent(this, ImageListView.class));
}
}

1 个答案:

答案 0 :(得分:0)

代码对我来说看起来不错。似乎是一个权限问题。将这些行添加到PHP脚本的顶部。

<?php
ini_set('display_errors', 1); // telling PHP to explicitly print all errors
error_reporting(E_ALL); // Show all notices, warnings and fatal errors

第二件事,检查文件夹是否具有足够的权限来写入文件。如果不存在,请授予写权限,然后查看代码是否有效。

希望这会有所帮助。编码愉快!