2017-04-26 58 views
1

这是我的代码。它共享相同的旧屏幕截图,我如何删除旧的屏幕截图并生成一个新的屏幕截图。而且在Facebook上分享时,它不会添加我提供的链接。它仅在whatsapp上的Facebook上共享屏幕截图,但它共享相同的旧图片。生成新的屏幕截图

public Bitmap takeScreenshot() { 
     View rootView = findViewById(android.R.id.content).getRootView(); 
     rootView.setDrawingCacheEnabled(true); 
     return rootView.getDrawingCache(); 
} 

public void saveBitmap(Bitmap bitmap) { 
    imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png"); 
    FileOutputStream fos; 
    try { 
     fos = new FileOutputStream(imagePath); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
     fos.flush(); 
     fos.close(); 
    } catch (IOException e) { 
     Log.e("GREC", e.getMessage(), e); 
    }} 
private void shareIt() { 

    Uri uri = Uri.fromFile(imagePath); 
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
    sharingIntent.setType("image/*"); 
    String shareBody = "Can you solve this , I am stuck/n try your answer at www.shackless.shan.com"; 

    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); 
    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri); 

    startActivity(Intent.createChooser(sharingIntent, "Challenge via")); 
} 
public void shareOnWhatsapp(View view) { 
    Bitmap bitmap = takeScreenshot(); 
    saveBitmap(bitmap); 
    shareIt(); 
} 

回答

0

我得到了解决方案,我必须inValidate绘图缓存。使用view.invalidate() befre setDrawingCacheEnabled(true)。

public Bitmap takeScreenshot() { 
     View rootView = findViewById(android.R.id.content).getRootView(); 

     rootView.invalidate(); 
     rootView.setDrawingCacheEnabled(true); 
     return rootView.getDrawingCache(); 
    }