Java – Publish an invalid MIME type for an image

Publish an invalid MIME type for an image… here is a solution to the problem.

Publish an invalid MIME type for an image

I’m trying to publish pictures from my gallery to a server from my Android device.
They use Python in the background.

This is what the background developers say:
– Django can’t read Android apps in request. Files published in FILES. iOS does this correctly.
– It seems that Multipart POST is not properly set to the key:value required to read the request correctly.

I’m getting this error :

{“errorMessage”:””,”message”:”Invalid mime
type”,”errorCode”:0,”success”:false}

You know why?

Here is my code :

public static final String IMAGE_JPEG = "image/jpeg";

private HttpEntity getImageEntity() throws Exception {

File imageFile;
            Uri originalUri = Uri.parse(this.mFileName);
            String originalPath = originalUri.getPath();
            boolean isEncrypted = originalPath.contains(FileNames.CACHE_DIR.getPath()); 
             check if file encrypted or not
            if (isEncrypted && ImageLoader.IMAGE_CODING_ENABLED) {
                File originalImageFile = new File(originalPath);
                String decodedPath = CipherUtils.decryptFile(SmartPagerApplication.getInstance(), originalImageFile);
                imageFile = new File(decodedPath);
            } else {
                imageFile = new File(originalPath);
            }

InputStream fis = imageFile.toURI().toURL().openStream();

int rotation = PhotoFileUtils.getOrientation(this.mFileName);
            if (rotation > 0) {
                byte[] data;
                Bitmap rotateBitmap = PhotoFileUtils.checkOrientation(BitmapFactory.decodeStream(fis), rotation);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                rotateBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                data = stream.toByteArray();
                stream.close();
                rotateBitmap.recycle();
                fis.close();
                fis = new ByteArrayInputStream(data);
            } else {
                 data = IOUtils.toByteArray(fis);
            }
            return getMultipartEntity(originalUri, fis);
}

private MultipartEntity getMultipartEntity(Uri originalPath, InputStream fis) {

InputStreamBody isb = new InputStreamBody(fis, mMimeType, originalPath.getLastPathSegment());

MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
                    "----WebKitFormBoundaryzGJGBFWyGteE24tw", Charset.forName("ISO-8859-1"));
            multipartEntity.addPart("binaryFile", isb);
            return multipartEntity;
}

private String executePost(String url, HttpEntity params) throws ClientProtocolException, IOException {
            Log.e("executePost url =" + url);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-Type", "multipart/form-data; boundary="+"----WebKitFormBoundaryzGJGBFWyGteE24tw");
            httpPost.addHeader("Cache-Control", "no-cache");
            httpPost.setEntity(params);

String response =  SmartPagerHTTPClient.getHttpClient().execute(httpPost, new BasicResponseHandler());

return response;
 }

Solution

I

don’t have enough reputation to comment, so I have to take this as an answer. In your method getMultipartEntity(), the first line:

InputStreamBody isb = new InputStreamBody(fis, mMimeType,  originalPath.getLastPathSegment());

What is the value of mMimeType? Make sure it is the correct MIME type.

Related Problems and Solutions