0

I want to send the bitmap of the images to the service class and i want to use that bitmap there to set wallpaper The code i used is....

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PICK_IMAGE_MULTIPLE) {
                imagesPathList = new ArrayList<String>();
                String[] imagesPath = data.getStringExtra("data").split("\\|");
                try {
                    lnrImages.removeAllViews();
                } catch (Throwable e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < imagesPath.length; i++) {
                    imagesPathList.add(imagesPath[i]);
                    yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
                    resized[i] = Bitmap.createScaledBitmap(yourbitmap, 480,
                            800, true);
                    ImageView imageView = new ImageView(this);
                    imageView.setImageBitmap(resized[i]);
                    imageView.setAdjustViewBounds(true);
                    lnrImages.addView(imageView);
                }
            }
        }

    }



case R.id.btnsetwall:
            Intent i = new Intent(MainActivity.this, WallService.class);
            Log.i("Main Activity", "Before putExtra");
            i.putExtra("Imagess", resized);
            Log.i("Main Activity", "After putExtra");
            startService(i);
            Log.i("Main Activity", "Start Service");
            break;

//If i use i.putExtra("Imagess", resized); it gives error and if i dont use this line service gets started

In service class...

@Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.i("on create", "Service Created");

    }

The logcat is...

05-06 15:53:57.932: I/Main Activity(14185): Before putExtra
05-06 15:53:57.932: I/Main Activity(14185): After putExtra
05-06 15:53:57.942: E/JavaBinder(14185): !!! FAILED BINDER TRANSACTION !!!
05-06 15:53:57.952: I/Main Activity(14185): Start Service
user2672165
  • 2,986
  • 19
  • 27
nawaab saab
  • 1,892
  • 2
  • 20
  • 36

3 Answers3

0

Bitmap class itself implemented parcelable interface. So you can pass array of bitmaps just by calling Bitmap[] btmaps = new Bitmap[10]; Intent intent = new Intent(); intent.putExtra("bitmaps", btmaps);

And you can receive this array through intent as

Bitmap[] btmpList = (Bitmap[]) intent.getParcelableArrayExtra("bitmaps");
subair_a
  • 1,028
  • 2
  • 14
  • 19
0

If you want to use a bitmap in different areas of your app you do not pass them around. You are on a very limited memory machine keep a bitmap as little time as possible in the heap.

The way you do it, you just save it to a file temporarly and then load it whereever you need it again.

Get your cache dir

public static String getCacheDir(Context ctx) {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||!Environment.isExternalStorageRemovable() ?
                ctx.getExternalCacheDir().getPath() : ctx.getCacheDir().getPath();
    }

Save the bitmap

public static File saveBitmap(Bitmap bitmap, String filename, String path, boolean recycle) {
        FileOutputStream out=null;
        try {
            File f = new File(path,filename);
            if(!f.exists()) {
                f.createNewFile();
            }
            out = new FileOutputStream(f);
            if(bitmap.compress(Bitmap.CompressFormat.PNG, 90, out)) {
                return f;
            }
        } catch (Exception e) {
            Log.e(TAG, "Could not save bitmap", e);
        } finally {
            try{
                out.close();
            } catch(Throwable ignore) {}
            if(recycle) {
                bitmap.recycle();
            }
        }
        return null;
    }

Then load it again.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
selected_photo.setImageBitmap(bitmap);

You could also look into Picasso which handles async loading for you and also enhances Performance with a disk cache and many more features

Picasso.with(getContext()).load(bitmapFile).into(imageView);
Patrick
  • 33,984
  • 10
  • 106
  • 126
-1

you can solve this in different different way with your requirement.

  1. u can use static variable for storing imageBitmap

  2. u can pass with intent

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);    
    byte[] byteArray = stream.toByteArray();    
    Intent in1 = new Intent(this, Activity2.class);    
    in1.putExtra("image",byteArray);
    
duggu
  • 37,851
  • 12
  • 116
  • 113
SuN
  • 1,723
  • 1
  • 13
  • 14