我正在尝试将照片从我的android设备上传到服务器。但是我似乎无法理解我刚拍摄的图像的路径,有人知道这样做的方法吗?
这是我使用的代码示例。我想使用onActivityResult
方法设置照片路径,并使用uploadImage()
发送照片;
谢谢。
public class MainActivity extends AppCompatActivity {
@BindView(R.id.btn)
Button button;
@BindView(R.id.pic_preview)
ImageView picPreview;
public static final String UPLOAD_URL = "http://ip/folder/upload.php";
private static final int REQUEST_IMAGE_CAPTURE = 101;
private String name;
private Uri filePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.btn)
public void takePicture(){
Intent takePic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePic.resolveActivity(getPackageManager()) != null){
startActivityForResult(takePic, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
filePath = data.getData();
//Get Image path here
//Bundle extras = data.getExtras();
//Bitmap imageBitmap = (Bitmap) extras.get("data");
//picPreview.setImageBitmap(imageBitmap);
}
}
private void uploadImage(){
String location = locationString;
String name = "someName";
String path = filePath.getPath();
try{
String uploadId = UUID.randomUUID().toString();
new MultipartUploadRequest(this, uploadId, UPLOAD_URL)
.addFileToUpload(path, "image")
.addParameter("name", name)
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(3)
.startUpload();
}catch (Exception ex){
}
}