将屏幕内容作为图像保存到内部存储器中,并以编程方式与社交媒体共享

时间:2014-11-11 10:28:49

标签: android save storage screenshot internal

下面的代码将捕获屏幕并将其存储在SD卡中。我希望然后它会通过可共享的应用程序发送此文件。我想将其存储在手机的内部存储器中,但我无法做到这一点。请帮助我同样的。

    WebView view = (WebView) findViewById(R.id.webView1);
    @SuppressWarnings("deprecation")
    Picture picture = view.capturePicture(); 

    Bitmap  b = Bitmap.createBitmap( picture.getWidth(), 
            picture.getHeight(), Bitmap.Config.ARGB_8888); 

    Canvas c = new Canvas( b ); 

    picture.draw( c ); 

    String filePath = Environment.getExternalStorageDirectory()
            + File.separator + "score.png";
    File imagePath = new File(filePath);
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        if(fos!=null)
        {
        b.compress(CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
        }
        if(imagePath.exists())
        {
        sendMail(filePath);
        }
        else
        {
            Log.e("fie","file doesnt exist");
        }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }

public void sendMail(String path) {
        Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        //emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
           //     new String[] { "youremail@website.com" });
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                "My Score in Mock Test");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                "PFA");
        emailIntent.setType("image/png");
        Uri myUri = Uri.parse("file://" + path);
        emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
        startActivity(Intent.createChooser(emailIntent, "share score card..."));
    }

我尝试使用堆栈溢出时发现的以下代码来实现此目的,但它无法正常工作。当我通过它调试但是文件没有创建到内部存储器而因此发送失败时,此代码没有显示任何异常。

0 个答案:

没有答案