0

I downloaded one image from server in local network using Glide in AsyncTask. In postExecute I tried to display the downloaded bitmap and it worked fine. But when I convert the bitmap into Pix object and pass it to another activity, the passed Pix object is Null. Below is my code

public void onResponse(String s) {    //onResponse of a Volley Request
                    progressDialog.dismiss();
                    if (s.equals("OK")) {
                        Toast.makeText(ImageUploadDialog.this, "Uploaded Successful", Toast.LENGTH_LONG).show();
                        final ImageView image = (ImageView) findViewById(R.id.myImageView);
                        final Bitmap[] bmp = {null};
                        new AsyncTask<Void, Void, Void>() {
                            private static final String TAG = "STARTING_ACTIVITY";
                            @Override
                            protected Void doInBackground(Void... params) {
                                Looper.prepare();
                                try {
                                    bmp[0] = Glide.
                                            with(ImageUploadDialog.this).
                                            asBitmap().
                                            load("http://192.168.0.8:80/static/images/result.png").
                                            into(1024, 666).
                                            get();
                                } catch (final ExecutionException e) {
                                    Log.e(TAG, e.getMessage());
                                } catch (final InterruptedException e) {
                                    Log.e(TAG, e.getMessage());
                                }
                                return null;
                            }
                            @Override
                            protected void onPostExecute(Void dummy) {
                                if (null != bmp[0]) {
                                    // The full bitmap should be available here
                                    //image.setImageBitmap(bmp[0]);
                                    Log.d(TAG, "Image loaded");
                                    Pix pix = ReadFile.readBitmap(bmp[0]);
                                    Pix copiedPix = pix.copy();
                                    Intent result = new Intent();
                                    result.putExtra(UPLOAD_IMAGE, true);
                                    result.putExtra(EXTRA_NATIVE_PIX, copiedPix.getNativePix());
                                    result.putExtra(OCRActivity.EXTRA_USE_ACCESSIBILITY_MODE, accessibilityMode);
                                    setResult(RESULT_OK, result);
                                    finish();
                                };
                            }
                        }.execute();
Zoe
  • 23,712
  • 16
  • 99
  • 132
  • Is bmp[0] not null in doInBackground after you do the get? – user2199860 May 20 '20 at 15:52
  • It is not Null. Reason - I tried to display the bitmap in the onPostExecute() method and it worked fine. See the commented line in onPostExecute(). – Shreyas Ambekar May 20 '20 at 16:04
  • I guess you can pass Bitmap directly https://stackoverflow.com/questions/2459524/how-can-i-pass-a-bitmap-object-from-one-activity-to-another?rq=1 – Raghul Vaikundam May 20 '20 at 17:15
  • I'm a little confused by this line: final Bitmap[] bmp = {null}; Could you try it with a 1 element Bitmap array instead? final Bitmap[] bmp = new Bitmap[1](); – user2199860 May 20 '20 at 17:30
  • And just as a side note. Glide is already async, you don't need the AsyncTask. You could just put your onPostExecute code into the Glide onResourceReady callback. – user2199860 May 20 '20 at 17:35
  • @user2199860 Yes I had tried that too... Finally it was possible to pass the data available in onResponse() method of volley after implementing one interface... I referred this link - https://mobikul.com/how-to-send-json-post-request-using-volley-rest-api/ – Shreyas Ambekar May 22 '20 at 06:43

0 Answers0