原创

如何使用片段中的媒体管理器将图像发布到 cloudinary?

温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I have downloaded an Image Picker Library that displays my gallery and crops images for me etc. When I receive the URI from the ActivityResultLauncher I try to upload the image that I had just chosen to Cloudinary. However, it never even enters the function as I found out by adding log statements. I have no idea why this is. I even tried to do it asynchronously and still nothing. The Uri is being received as when I load it into the Image View it loads for me.

Here is my ActivitResultLauncher

    ActivityResultLauncher<Intent> launcher=
            registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),(ActivityResult result)->{
                if(result.getResultCode()==RESULT_OK){
                    Uri uri=result.getData().getData();
                    Picasso.get().load(uri).into(profilePic);
                    Log.d(TAG, "Before uploading image:  ");
                    new UploadTask(uri).execute();
                }else if(result.getResultCode()==ImagePicker.RESULT_ERROR){
                    ImagePicker.Companion.getError(result.getData());
                }
                Log.d(TAG, "afterUploading image");
            });

Here is my Async class

    public class UploadTask extends AsyncTask<Void, Void, Void> {
        private Uri uri;
        ProgressDialog progressDialog = new ProgressDialog(requireContext());
        public UploadTask(Uri uri){
            this.uri = uri;
        }

        @Override
        protected void onPreExecute() {
            progressDialog.show();
            Log.d(TAG, "onPreExecute method: " + uri);
        }

        @Override
        protected Void doInBackground( Void... voids ) {
            uploadCloud( uri);
            return null;
        }

        @Override
        protected void onPostExecute( Void res ) {
            progressDialog.hide();
        }
    }

And finally here is the upload to Cloudinary function

    public void uploadCloud(Uri uri) {
        MediaManager.get().upload(uri).callback(new UploadCallback() {
            @Override
            public void onStart(String requestId) {
                Log.d(TAG, "onStart method");
            }

            @Override
            public void onProgress(String requestId, long bytes, long totalBytes) {
                Log.d(TAG, "onProgress method");
            }

            @Override
            public void onSuccess(String requestId, Map resultData) {
                Log.d(TAG, "onSuccess method");
            }

            @Override
            public void onError(String requestId, ErrorInfo error) {
                Log.d(TAG, "onError: ");
            }

            @Override
            public void onReschedule(String requestId, ErrorInfo error) {
                Log.d(TAG, "onReschedule method");
            }
        });
    }

If somebody can point me in the right direction it would be greatly appreciated!

I already have the Cloudinary working in other parts of my project so it has nothing to do with Initialising the MediaManager correctly or anything.

正文到此结束
热门推荐
本文目录