Question

I'm trying to switch a banner adView to imageView just before I take a screenshot so that users can share this screenshot through share intent. However, when I take the screenshot, it does not include the imageView.

public void onCreate(final Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ....
  adView1 = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID1);
  LinearLayout.LayoutParams childParam2 = new    
  LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 0.10f);
  adView1.setLayoutParams(childParam2);
  adView1.loadAd(new AdRequest());
  ll.addView(adView1);
  setContentView(ll);


  myAdView = new ImageView(this);
  LinearLayout.LayoutParams childParam1 = new    
       LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 0.10f);
  myAdView.setLayoutParams(childParam1);

  ....
  View.OnClickListener handler = new  View.OnClickListener() {
        public void onClick(View v) {
             switch (v.getId()) {
                       ...
                       case R.id.menu3:
                           share();
                           break;
                       ...
                     }
 }

Here's share() function.

private void share(){
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("*/*");
    List<ResolveInfo> resInfo =     
                 this.getPackageManager().queryIntentActivities(intent, 0);

    for (ResolveInfo resolveInfo : resInfo) {
            ........

                if (packageName.toLowerCase().contains("twitter")){

                  targetedShareIntent.setType("*/*");
                  String location = "file://" + takeScreen(ll);
                   ...
                 }
            ...
    }

This is takeScreen(View v) function.

public String takeScreen(View c_view){
     ll.removeView(adView1);
     ll.addView(myAdView);

    // create bitmap screen capture
    Bitmap bitmap;
    View v1 = c_view.getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = v1.getDrawingCache();

    String extr = Environment.getExternalStorageDirectory().toString();
    File imageFile = new File(extr,  "screen_" + System.currentTimeMillis() + ".jpg");

    OutputStream fout = null;

    try {
        fout = new FileOutputStream(imageFile);
        boolean saved = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
        //Log.e("bitmap saved ?", saved + "!");
        fout.flush();
        fout.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Screen", "screen");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    ll.removeView(myAdView);
    ll.addView(adView1);
    return imageFile.getPath();
}

As you can see, I'm removing adView and adding myAdView(imageView) just before the screenshot is taken in takeScreen() function. adView IS removed but imageVies is NOT added to the screenshot.

The imageView DOES appear on the screen just before chooserIntent(share intent) pop-up screen is displayed.

I have tried many other options like

  • added both views and just switched visibility. setVisibility(View.Gone, View.Visible)
  • tried creating bitmap with canvas instead of getDrawingCache (thinking that it could be a cache related problem)

Is taking screenshot or 'share intent' too much of work for the UI thread to be blocked? Can anyone shed a light here? I am completely at a loss.

Was it helpful?

Solution

I found a way to get around this. I created a composite bitmap out of the background bitmap and the overlay(my ad image) bitmap. In case anyone is interested, here's the code.

public Bitmap screenShot(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    Bitmap overlay = BitmapFactory.decodeResource(this.getResources() , R.drawable.my_ad);

    canvas.drawBitmap(overlay, 100, 100, null);
    return bitmap;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top