getBitmap()方法不会保存屏幕截图

时间:2018-11-14 15:01:11

标签: android file screenshot fileoutputstream

下午好,在我的应用程序中,我使用移动摄像头拍摄了视频,然后我开始播放此视频

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
    Surface s = new Surface(surface);

    try
    {
        mp = new MediaPlayer();
        mp.setDataSource(getVideoFilePath());
        mp.setSurface(s);
        mp.prepare();

        mp.setOnBufferingUpdateListener(this);
        mp.setOnCompletionListener(this);
        mp.setOnPreparedListener(this);
        mp.setOnVideoSizeChangedListener(this);

        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        setPreviewSize(true);
        mp.start();
    }
    catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我实现了一种方法,该方法可以截取屏幕截图并将其保存到图库中,

  public void getBitmap(TextureView vv)
{
    String mPath = getAndroidImageFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "cameraRecorder.png";
    Toast.makeText(getApplicationContext(), "Capturing Screenshot: " + mPath, Toast.LENGTH_SHORT).show();

    Bitmap bm = vv.getBitmap();
    if(bm == null)
        Log.e(TAG,"bitmap is null");

    OutputStream fout = null;
    File imageFile = new File(mPath);

    try {
        fout = new FileOutputStream(imageFile);
        bm.compress(Bitmap.CompressFormat.PNG, 90, fout);
        fout.flush();
        fout.close();
    } catch (FileNotFoundException e) {
        Log.e(TAG, "FileNotFoundException");
        e.printStackTrace();
    } catch (IOException e) {
        Log.e(TAG, "IOException");
        e.printStackTrace();
    }
}

但是屏幕截图没有出现在图库中,并导致错误。 这就是Logcat产生的结果

 E/TextureViewActivity: FileNotFoundException
11-14 15:25:09.961 4143-4143/com. W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Pictures/201811_14-152509cameraRecorder.png: open failed: ENOENT (No such file or directory)
11-14 15:25:09.971 4143-4143/com. W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
11-14 15:25:09.971 4143-4143/com. W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
11-14 15:25:09.971 4143-4143/com. W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
11-14 15:25:09.971 4143-4143/com. W/System.err:     at com. 
 camera.VideoProcessor.getBitmap(VideoProcessor.java:245)
11-14 15:25:09.971 4143-4143/com. W/System.err:     at com.camera.VideoProcessor.onClick(VideoProcessor.java:369)
  

此处代码的哪一部分会引发错误

at com.camera.VideoProcessor.getBitmap(VideoProcessor.java:247)  ->  fout = new FileOutputStream(imageFile);

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

听起来您没有正确的权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />添加到AndroidManifest.xml中(如果尚不存在的话)。