私はアンドロイドスタジオを初めて利用しています。私はアプリとウェブサイトを開発しています。画像をキャプチャするためのアプリ、管理者向けのウェブサイト。アプリのアクティビティには、画像のキャプチャ、場所の検出、タイムスタンプの検出、名前の挿入、コメントの挿入が含まれます。android studioを使用して文字列データを含む画像をmysqlサーバにアップロードするには
問題は、データの場所、時刻、名前、コメントを私のサーバのMySQLにアップロードしたいときです。それは動作しません。ここで
は、PHPの私のコーディングです:ここでは
<?php
$con = mysqli_connect("localhost", "hafiza", "98765432i", "trackerproject");
$rid = $_POST["rid"]
$rname = $_POST["rname"];
$rimage = $_POST["rimage"];
$rlocation = $_POST["rlocation"];
$rdate = $_POST["rdate"];
$rcomment = $_POST["rcomment"];
$rstatus = $_POST["rstatus"];
$statement ="SELECT rid FROM report ORDER BY rid ASC";
$res = mysqli_query($con,$statement);
$rid = 0;
while($row = mysqli_fetch_array($res)){
$rid = $row['rid'];
}
$path = "uploads/$id.png";
$actualpath = "http://hafizahusairi.com/trackerproject/$path";
$statement = mysqli_prepare($con, "INSERT INTO report (rname, rimage, rlocation, rdate, rcomment, rstatus) VALUES (?, ?, ?, ?, ?, ?='New',)");
mysqli_stmt_bind_param($statement, "ssssss", $rname, $rimage, $rlocation, $rdate, $rcomment, $rstatus);
mysqli_stmt_execute($statement);
if(mysqli_query($con,$statement)){
file_put_contents($path,base64_decode($rimage));
echo "Successfully Uploaded";
}
mysqli_close($con);
}else{
echo "Error";
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
は私のアンドロイドスタジオの符号化です。
package com.hafizahusairi.cameratracker;
import android.Manifest;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
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 org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;
public class Display extends AppCompatActivity implements View.OnClickListener{
//location
static final int REQUEST_LOCATION = 1;
LocationManager locationManager;
//Date and Time
Calendar calander;
SimpleDateFormat simpledateformat;
EditText etTimeDate;
Button btnDateTime;
private Button bSelectPhoto;
private Button bReportSubmit;
private ImageView ivDisplayImage;
private EditText etLocation;
private EditText etName;
private EditText etComment;
private Bitmap bitmap;
private int PICK_IMAGE_REQUEST = 1;
private String UPLOAD_URL ="http://hafizahusairi.com/trackerproject/photoUpload/upload.php";
private String rdate = "rdate";
private String rname = "rname";
private String rimage = "rimage";
private String rlocation = "rlocation";
private String rcomment = "rcomment";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
//button
bSelectPhoto = (Button) findViewById(R.id.bSelectPhoto);
bReportSubmit = (Button) findViewById(R.id.bReportSubmit);
btnDateTime = (Button)findViewById(R.id.btnDateTime);
ivDisplayImage = (ImageView) findViewById(R.id.ivDisplayImage);
etLocation = (EditText) findViewById(R.id.etLocation);
etName = (EditText) findViewById(R.id.etName);
etComment = (EditText) findViewById(R.id.etComment);
//button onclick
bSelectPhoto.setOnClickListener(this);
bReportSubmit.setOnClickListener(this);
etTimeDate = (EditText) findViewById(R.id.etTimeDate);
//Location
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
getLocation();
calander = Calendar.getInstance();
simpledateformat = new SimpleDateFormat("dd-MM-yyyy , HH:mm:ss");
rdate = simpledateformat.format(calander.getTime());
btnDateTime.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
etTimeDate.setText(rdate);
}
});
}
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;
}
//tak dapat upload image dengan string params
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(Display.this, s , Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
//Showing toast
Toast.makeText(Display.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String rimage = getStringImage(bitmap);
//Getting Image Name
String rname = etName.getText().toString().trim();
String rlocation = etLocation.getText().toString().trim();
String rdate = etTimeDate.getText().toString().trim();
String rcomment = etComment.getText().toString().trim();
//Creating parameters
Map<String,String> params = new Hashtable<String, String>();
//Adding parameters
params.put(rname, rname);
params.put(rimage, rimage);
params.put(rlocation, rlocation);
params.put(rdate, rdate);
params.put(rcomment, rcomment);
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
//select image dah OK!
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) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
ivDisplayImage.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
//detect location dah OK!
void getLocation() {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
double latti = location.getLatitude();
double longi = location.getLongitude();
((EditText)findViewById(R.id.etLocation)).setText(latti + "," + longi);
} else {
((EditText)findViewById(R.id.etLocation)).setText("Unable to find current location. Try again");
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_LOCATION:
getLocation();
break;
}
}
@Override
public void onClick(View v) {
if(v == bSelectPhoto){
showFileChooser();
}
if(v == bReportSubmit){
uploadImage();
}
}
}
私のデータベースには、次のようになります。
あなたのイメージのビットマップを取得し、文字列にビットマップを変換し、サーバに文字列を送信する必要があり、サーバに画像を保存し、サーバからのため
の可能性のある重複ます(http:// stackoverflowの.com/questions/4830711/how-to-convert-a-image-base64-stringへ) – jagapathi