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



下午好,在我的应用程序中,我用移动相机拍摄视频,然后我启动了这个视频,感谢

@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);

我将非常感谢你的帮助(

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />添加到AndroidManifest.xml中(如果它还没有(。

最新更新