此代码在android 6.0及以下版本中正常运行,但不适用于android 7.0及以上版本中

时间:2019-11-19 18:51:55

标签: android android-intent

此代码在android 6.0及更低版本中可以正常使用,但不适用于android 7.0及更高版本中。我已将URI更改为文件提供程序。该代码用于获取和共享屏幕截图。 预先感谢。

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


public void onClick(View v) {
    Bitmap bitmap = takeScreenshot();
    saveBitmap(bitmap);
    shareIt();
}
public void saveBitmap(Bitmap bitmap) {
    WebView webview=(WebView)findViewById(R.id.web1);
    imagePath = new File(Environment.getExternalStorageDirectory() +"/"+ webview.getTitle().toString()+".png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}
private void shareIt() {
    WebView webView=(WebView)findViewById(R.id.web1);
    // Uri uri = Uri.fromFile(imagePath);
    Uri uri = FileProvider.getUriForFile(this,"harsh.akshr.oneclickaccess",imagePath);
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("image/*");
    String shareBody = webView.getTitle().toString();
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,  webView.getTitle().toString());
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(sharingIntent, "Share ScreenShot via"));
}

0 个答案:

没有答案