我想使用codeigniter rest服务器保存图片并通过android studio上传。我想使用签名板创建电子签名,并将电子签名保存到服务器中,然后将文件名保存到数据库中。我已经在智能手机中创建了一个签名板并成功保存文件,但是无法将文件上传到服务器中。我已经使用邮差及其工作来测试myPhp函数。
我的ApiInterface:
@Multipart
@POST("Technician/createSuratPerintahKerja")
Call<PostData> uploadImage(@Part MultipartBody.Part sign_installer);
我的Java函数上载:
bitmap = signaturePad.getSignatureBitmap();
path = saveImage(bitmap);
File file = new File(path);
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("sign_installer", path, requestBody);
ApiInterface api = ApiClient.getClient().create(ApiInterface.class);
Call<PostData> call = api.uploadImage(body);
call.enqueue(new Callback<PostData>() {
@Override
public void onResponse(Call<PostData> call, Response<PostData> response) {
Toast.makeText(getApplicationContext(),"True", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<PostData> call, Throwable t) {
Toast.makeText(getApplicationContext(),"Not Response",Toast.LENGTH_SHORT).show();
}
});
功能SaveImage:
public String saveImage(Bitmap myBitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File wallpaperDirectory = new File(
Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY /*iDyme folder*/);
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".png");
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists()) {
wallpaperDirectory.mkdirs();
Log.d("hhhhh",wallpaperDirectory.toString());
}
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(SpkActivity.this,
new String[]{f.getPath()},
new String[]{"image/png"}, null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
return f.getAbsolutePath();
} catch (IOException e1) {
e1.printStackTrace();
}
return f.getAbsolutePath();
}
我的PHP功能上传:
function uploadfile(){
$config['upload_path'] = './uploads/ba/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = "example1";
$this->upload->initialize($config);
if(!$this->upload->do_upload('sign_installer')){
$respond['error'] = true;
$respond['message'] = "Error";
exit();
}
}