I have an application that creates a file as follows:
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
storageDir // directory
);
Following this calling image.exists() returns true. I then send the file name as extra data in an intent to the camera:
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
I save the file path which looks like this: "file:/storage/emulated/0/Pictures/JPEG_20171211_162442_-1697405777.jpg". When the result returns from the Camera activity, I can create a bitmap using the file path:
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
I successfully display the bitmap, but when I create a File object using the path, File.exists() returns false, despite the fact that I can see the file using "adb shell ls". More to the point, I cannot load the image to a server using a POST call, which looks like this:
File sourceFile = new File(sourceImageFile);
String fileName = sourceFile.getName();
Log.d(Constants.TAG, "File...::::" + sourceFile + " : " + sourceFile.exists());
final MediaType MEDIA_TYPE = MediaType.parse("image/jpeg");
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("xxx_upload_file", fileName, RequestBody.create(MEDIA_TYPE, sourceFile))
.addFormDataPart("content", "upload_img")
.addFormDataPart("XXX_id", Constants.xxx_ID)
.addFormDataPart("session_id", mPicActivity.mSessionID)
.addFormDataPart("source_url", "Chatbot")
.build();
Request request = new Request.Builder()
.url(Constants.CHATBOT_URL_BASE + Constants.STORE_EXAMINE)
.post(requestBody)
.build();
mClient.newCall(request).enqueue(new Callback()...);
The server responds to other calls, so it is not a connectivity or URL issue. Is the upload failing because the file suddenly does not exist (even though it does exist, as mentioned above)? Is the issue connected to the "emulated" file path.
"file:/storage/emulated/0/Pictures/JPEG_20171211_162442_-1697405777.jpg".
That should be
"/storage/emulated/0/Pictures/JPEG_20171211_162442_-1697405777.jpg".