最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How can I integrate Supabase Storage into my Android app on the client side to store data like images? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to integrate Supabase Storage into my Android app project. I have a feature that requires storing an image as a user's profile picture, but I keep encountering an 'Upload Error' with a 404 response code from the Supabase server.

Logcat Errors:

Response Code: 404 - <!DOCTYPE html>
                                                                                                    <html style="height:100%">
                                                                                                    <head>
                                                                                                    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
                                                                                                    <title> 404 Not Found
                                                                                                    </title></head>
                                                                                                    <body style="color: #444; margin:0;font: normal 14px/20px Arial, Helvetica, sans-serif; height:100%; background-color: #fff;">
                                                                                                    <div style="height:auto; min-height:100%; ">     <div style="text-align: center; width:800px; margin-left: -400px; position:absolute; top: 30%; left:50%;">
                                                                                                            <h1 style="margin:0; font-size:150px; line-height:150px; font-weight:bold;">404</h1>
                                                                                                    <h2 style="margin-top:20px;font-size: 30px;">Not Found
                                                                                                    </h2>
                                                                                                    <p>The resource requested could not be found on this server!</p>
                                                                                                    </div></div><div style="color:#f0f0f0; font-size:12px;margin:auto;padding:0px 30px 0px 30px;position:relative;clear:both;height:100px;margin-top:-101px;background-color:#474747;border-top: 1px solid rgba(0,0,0,0.15);box-shadow: 0 1px 0 rgba(255, 255, 255, 0.3) inset;">
                                                                                                    <br>Proudly powered by  <a style="color:#fff;" href=";>LiteSpeed Web Server</a><p>Please be advised that LiteSpeed Technologies Inc. is not a web hosting company and, as such, has no control over content found on this site.</p></div></body></html>

I tried looking for a solution on the internet, especially on how to integrate Supabase into an Android application, but none of them worked for me.

I have an ApiService.java

@Multipart
    @PUT("{bucket}/{fileName}")
    Call<ResponseBody> uploadToS3(
            @Header("Authorization") String authToken,
            @Header("x-amz-acl") String acl, // Set to "public-read" if public
            @Header("Content-Type") String contentType,
            @Path("bucket") String bucket,
            @Path("fileName") String fileName,
            @Part MultipartBody.Part file
    );

and this is my code for uploading the image in the supabase server, but it is not working

private void uploadImageToSupabase(File imageFile) {
        if (imageFile.length() == 0) {
            Toast.makeText(getContext(), "Error: Empty file", Toast.LENGTH_SHORT).show();
            return;
        }

        String fileName = UUID.randomUUID().toString() + ".jpg";
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), imageFile);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", fileName, requestBody);

        ApiService apiService = ApiClient.getInstance().create(ApiService.class);

        Call<ResponseBody> call = apiService.uploadToS3(
                SUPABASE_KEY,  // Authorization token
                "public-read", // ACL: public or private
                "image/jpeg",  // Content-Type
                SUPABASE_BUCKET, // Bucket name
                fileName, // File name
                body
        );

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    String imageUrl = SUPABASE_URL + "/storage/v1/s3/" + SUPABASE_BUCKET + "/" + fileName;
                    saveImageDetails(imageUrl);
                    Toast.makeText(getContext(), "Upload successful!", Toast.LENGTH_SHORT).show();
                } else {
                    try {
                        String errorBody = response.errorBody().string();
                        Log.e("Upload Error", "Response Code: " + response.code() + " - " + errorBody);
                        Toast.makeText(getContext(), "Upload failed: " + errorBody, Toast.LENGTH_LONG).show();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(getContext(), "Error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论