我尝试使用改造从图库中上传图片。没有用,有人可以帮我吗? 我在下面附加代码
我也在本地主机上尝试了API。没有人为我工作。
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);
}
}
//另一个Java类 公共类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;
}
}
//另一个Java类 公共类ImageClass {
@SerializedName("phn")
private String phn;
@SerializedName("image")
private String Image;
@SerializedName("response")
private String Response;
public String getResponse() {
return Response;
}
}
//另一个Java类 公共接口ApiInterface {
@FormUrlEncoded
@POST("upload.php")
Call<ImageClass> uploadImage(@Field("phn") String phn,@Field("image") String image);
}
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);
}
我想将图像发送到数据库/文件。 当我单击上载按钮时,它没有任何输出。 请帮忙。谢谢。