У меня есть android app
, где пользователь выбирает image
из gallery
, а затем, когда он сохраняет его, image
сохраняется в папке UPLOADS
на моем server
и в MySQL
таблица, изображение url
сохранено.
Я использую retrofit
для сохранения фотографии.
У меня проблема, когда пользователь выбирает photo
и сохраняет его,приложение возвращает ошибку End of input at line 1 column 1 path $
, , но фотография отлично сохраняется на сервере и URL на MySQL .
Но вместо получения End of input error at line 1 column 1 path $
я должен получить сообщение Toast.
Как я могу перестать получать это сообщение об ошибке?
MainActivity
public class MainActivity extends AppCompatActivity {
Bitmap bitmap;
ImageView imageView;
Button selectImg,uploadImg;
EditText imgTitle;
private static final int IMAGE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
selectImg = (Button) findViewById(R.id.button);
uploadImg = (Button) findViewById(R.id.button2);
imgTitle = (EditText) findViewById(R.id.editText);
selectImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
uploadImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validateImage();
}
});
}
private void validateImage() {
//find values
final String regName = imgTitle.getText().toString();
String image = convertToString();
// checking if username is empty
if (TextUtils.isEmpty(regName)) {
Toast.makeText(MainActivity.this, "Seleziona i metodi di pagamento del tuo locale.", Toast.LENGTH_LONG).show();
return;
}
//checking if email is empty
//checking if password is empty
//After Validating we register User
uploadImage(regName, image);
}
private void selectImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, IMAGE);
}
private String convertToString()
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
byte[] imgByte = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte,Base64.DEFAULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode== IMAGE && resultCode==RESULT_OK && data!=null)
{
Uri path = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),path);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void uploadImage(String imageNome, String image){
ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<Img_Pojo> call = apiInterface.uploadImage(imageNome,image);
// ApiInterface apiInter = ApiClient.getApiClient().create(ApiInterface.class);
// Call<Img_Pojo> call = apiInter.uploadImage(imageName,image);
call.enqueue(new Callback<Img_Pojo>() {
@Override
public void onResponse(Call<Img_Pojo> call, Response<Img_Pojo> response) {
if(Objects.requireNonNull(response.body()).getIsSuccess() == 1) {
Toast.makeText(MainActivity.this, "Informazioni inserite!", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,response.body().getMessage(),Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<Img_Pojo> call, Throwable t) {
Toast.makeText(MainActivity.this,t.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}
ApiInterface
@FormUrlEncoded
@POST("up.php")
Call<Img_Pojo> uploadImage(@Field("image_name") String title, @Field("image") String image);
ApiClient
private static final String BaseUrl = "https://provaord.altervista.org/OrdPlace/image2/";
private static Retrofit retrofit;
public static Retrofit getApiClient() {
retrofit = new Retrofit.Builder().baseUrl(BaseUrl).
addConverterFactory(GsonConverterFactory.create()).build();
return retrofit;
}
Img_Pojo
private String Title;
private String Image;
private int isSuccess;
private String message;
public Img_Pojo(String Title, String Image, int isSuccess, String message) {
this.Title = Title;
this.Image = Image;
this.isSuccess = isSuccess;
this.message = message;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
this.Title = title;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
this.Image = image;
}
public int getIsSuccess() {
return isSuccess;
}
public void setIsSuccess(int success) {
this.isSuccess = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
up.php
<?php
$image_name = $_POST['image_name'];
$image = $_POST['image'];
$path = "imagini/$image_name.jpg";
$output=array();
//require database
require_once('db.php');
$conn=$dbh->prepare('INSERT INTO volley_upload(image_name,image_path) VALUES (?,?)');
//encrypting the password
$conn->bindParam(1,$image_name);
$conn->bindParam(2,$path);
$conn->execute();
file_put_contents($path,base64_decode($image));
if($conn->rowCount() == 0)
{
$output['isSuccess'] = 0;
$output['message'] = "Registrazione fallita, riprovare.";
}
elseif($conn->rowCount() !==0){
$output['isSuccess'] = 1;
$output['message'] = "Informazioni base inserite!";
}
?>