dongyi2534 2019-04-07 10:58
浏览 105

使用改造将图像上传到服务器

I tried to upload image from gallery using retrofit. It's not working could someone help me? I am attaching code below

I tried with API also with localhost. No one worked for me.

Java CODE:

public class ImagesendRetro extends AppCompatActivity implements View.OnClickListener{

private EditText  PhnNum;
private Button BnChhose,BnUpload;
private ImageView Img;
private static final int IMG_REQUEST=777;
private Bitmap bitmap;

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

 PhnNum=(EditText)findViewById(R.id.phnNumRetro);
 BnChhose=findViewById(R.id.chooseBnRetro);
 BnUpload=findViewById(R.id.uploadBnRetro);
 Img=(ImageView)findViewById(R.id.imageViewRetro);

 BnChhose.setOnClickListener(this);
 BnUpload.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.chooseBnRetro:
    selectImageRetro();
    break;
case R.id.uploadBnRetro:
    uploadImage();
    break;
default:
    break;
 }
 }



 private void uploadImage() {
 String Image = imageToString();
 String phnnumber = PhnNum.getText().toString();
 ApiInterface apiInterface = 
 ApiClient.getApiClient().create(ApiInterface.class);
 Call<ImageClass> call = apiInterface.uploadImage(phnnumber, Image);
 call.enqueue(new Callback<ImageClass>() {
     @Override
     public void onResponse(Call<ImageClass> call, Response<ImageClass> 
 response) {
         ImageClass imageClass = (ImageClass) response.body();

 Toast.makeText(ImagesendRetro.this,
 "Response"+imageClass.getResponse(),Toast.L 
 ENGTH_LONG).show();
         Img.setVisibility(View.GONE);
         PhnNum.setVisibility(View.GONE);
         BnChhose.setEnabled(true);
         BnUpload.setEnabled(false);
         PhnNum.setText("");

     }

     @Override
     public void onFailure(Call<ImageClass> call, Throwable t) {

     }
 });


}



private void selectImageRetro(){
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent,IMG_REQUEST);
    BnUpload.setVisibility(View.VISIBLE);
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
if (requestCode==IMG_REQUEST && resultCode==RESULT_OK && data!=null){
    Uri path=data.getData();
    try {
        bitmap= MediaStore.Images.Media.getBitmap(getContentResolver(),path);
        Img.setImageBitmap(bitmap);
        Img.setVisibility(View.VISIBLE);
        PhnNum.setVisibility(View.VISIBLE);
        BnUpload.setVisibility(View.VISIBLE);
        BnChhose.setEnabled(false);

    } catch (IOException e) {
        e.printStackTrace();
    }

}

}


private String imageToString(){
    ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
    byte[] imgByte=byteArrayOutputStream.toByteArray();
    return Base64.encodeToString(imgByte,Base64.DEFAULT);
}

}

//Another java class public class ApiClient {

private static final String BaseUrl="http://127.0.0.2:8080/imageupload/";
private static Retrofit retrofit;

public static Retrofit getApiClient(){
    if (retrofit==null){
        retrofit=new Retrofit.Builder().baseUrl(BaseUrl).
                addConverterFactory(GsonConverterFactory.create()).build();
    }
    return retrofit;
}

}

//Another java class public class ImageClass {

  @SerializedName("phn")
  private String phn;
  @SerializedName("image")
  private String Image;

  @SerializedName("response")
  private String Response;

  public String getResponse() {
    return Response;
  }

  }

//Another java class public interface ApiInterface {

@FormUrlEncoded
@POST("upload.php")
Call<ImageClass> uploadImage(@Field("phn") String phn,@Field("image") String image);

}

PHP CODE:

 if($con)
 {
 $title = $_POST['phn'];
 $image= $_POST['image'];
 $upload_path="uploads/$phn.jpg";
 $sql = "insert into imageinfo(phn,path) value('$phn','$upload_path');";


 if(mysqli_query($con,$sql))
{
file_put_contents($upload_path,base64_decode($image));
echo json_encode(array('response'=>"Image uploaded"));
}

 else
{
    echo json_encode(array('response'=>"Image upload Failed!"));
}
 mysql_close($con);
 }




 public interface ApiInterface {


@FormUrlEncoded
@POST("upload.php")
Call<ImageClass> uploadImage(@Field("phn") String phn,@Field("image") 
 String image);

 }

I want to send the image to the database/ files. When I click on Upload button it does not give any output. Please help. Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用