After selecting image,doc or pdf,I want to pass them through okhttp3.How do i go about doing this?
@Override
public void onActivityResult(int requestCode, in
So I am answering this question myself.
So I am firstly opening an outputstream to a temporary file.
imageTempUploadfile=new File(Environment.getExternalStorageDirectory()+"/inpaint/");
if(!imageTempUploadfile.exists())
imageTempUploadfile.mkdirs();
imageTempUploadfile2 = new File(Environment.getExternalStorageDirectory() + "/inpaint/"+"file"+".docx");
OutputStream outputTempFileStream = new FileOutputStream(imageTempUploadfile2);
The extension depends upon the mime type.You can find the mime type by
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==0){
Log.d("uri data",""+data.getData());
String mimetype = context.getContentResolver().getType(data.getData());
selectedFileUri = data.getData();
Now to read from the file and write its contents in the temporary file
selectedFileInputStream = getContentResolver().openInputStream(selectedFileUri);
Now to write into the file
try {
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = selectedFileInputStream.read(buffer)) != -1) {
outputTempFileStream.write(buffer, 0, read);
}
outputTempFileStream.flush();
} finally {
outputTempFileStream.close();
}
} catch (Exception e) {
e.printStackTrace(); // handle exception, define IOException and others
}
And finally
MultipartUtility multipart = new MultipartUtility(urlLeaveFormApply, charset);
multipart.addFilePart("data[User][user_picture]", imageTempUploadfile2);
At the end after success
imageTempUploadfile2.delete();