0

I am stuck to achieve below functionality in android actually I dont have much working experience in android new APIs (API 29)

1- Picked video from gallery, it gave me URI and I created IputeStreamRequestBody as per this thread

Here is my code for that:

public class InputStreamRequestBody extends RequestBody {

private final MediaType contentType;
private final ContentResolver contentResolver;
private final Uri uri;

public InputStreamRequestBody(MediaType contentType, ContentResolver contentResolver, Uri uri) {
    if (uri == null) throw new NullPointerException("uri == null");
    this.contentType = contentType;
    this.contentResolver = contentResolver;
    this.uri = uri;
}

@Nullable
@Override
public MediaType contentType() {
    return contentType;
}

@Override
public long contentLength() {
    return -1;
}

@Override
public void writeTo(@NonNull BufferedSink sink) throws IOException {
    try (Source source = Okio.source(contentResolver.openInputStream(uri))) {
        sink.writeAll(source);
    }
}

}

Step: 3- Here is my retrofit code:

public interface PlatformGroupPostService {

@Multipart
@POST("brainy/api/v0/post")
Observable<NewGroupAPIResponse> createPost(@Part("postData") RequestBody postData,
                                           @Part MultipartBody.Part attachments);

}

Request hit the server with null instance of file here is my Rest call:

//attachments always coming null
    public ResponseEntity<?> createPost(@RequestParam String postData,
                                        @RequestPart(value = "attachments", required = false)
                                                MultipartFile attachments) {
        PostEntity postEntity = getPostEntity(postData);
        return new ResponseEntity<>(postService.createPost(postEntity, attachments), HttpStatus.CREATED);
    }

above rest API is working fine with PostMan request but from Retrofit call its not working. I think I am doing something wrong.

I am struggling to achieve this feature since 4-5 days, applies multiple solution, Like

1- store android file from gallery to cache and then tries to upload it, but same issue. Now kind of overflow for me

fun prepareFilePart(partName: String, uri: Uri, context: Context, name: String): MultipartBody.Part {
    val inputStream = context.contentResolver.openInputStream(uri)
    val file = File(context.cacheDir, name+".mp4")
    val fos = FileOutputStream(file)


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        if (inputStream != null) {
            FileUtils.copy(inputStream, fos)
        }
    } else {

    }

    fos.flush();
    fos.getFD().sync();
    fos.close();
    val requestBody: RequestBody =
            RequestBody.create(MediaType.parse(context.contentResolver.getType(uri)), file)
    return MultipartBody.Part.createFormData(partName, name, requestBody)
}

enter image description here

any help would be really appreciable

Sandeep Tiwari
  • 2,042
  • 3
  • 24
  • 47
  • Try the second `InputStreamRequestBody` implementation in [this OkHttp GitHub issue comment](https://github.com/square/okhttp/issues/3585#issuecomment-327319196). If you read the full issue, including Jake Wharton's comments, you will see why `InputStreamRequestBody` needs to be able to *create* the `InputStream` (via a `Uri` and `ContentResolver`), not just wrap one. – CommonsWare Jun 09 '20 at 11:04
  • @CommonsWare didnt work that as well, its too complex for me – Sandeep Tiwari Jun 10 '20 at 16:04
  • @CommonsWare Can you help? https://stackoverflow.com/questions/62783444/why-does-multipart-pdf-is-not-able-to-upload-in-api-using-retrofit-2-in-android?noredirect=1#comment111031344_62783444 – Priyanka Singh Jul 08 '20 at 07:27
  • @SandeepTiwari Can you help? https://stackoverflow.com/questions/62783444/why-does-multipart-pdf-is-not-able-to-upload-in-api-using-retrofit-2-in-android?noredirect=1#comment111031344_62783444 – Priyanka Singh Jul 08 '20 at 07:27

1 Answers1

0

Hi after long digging I found solution and its working now:

Spring Boot multipartfile always null

Sandeep Tiwari
  • 2,042
  • 3
  • 24
  • 47