尝试捕获屏幕时,我收到java.io.FileNotFoundException

时间:2016-08-16 16:16:26

标签: android screen capture

我已将Android uses-permission文件中的所有manifest设置设置在应用标记之外:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

但是当我尝试捕获屏幕时,我收到以下错误:

  

java.io.FileNotFoundException:/storage/emulated/0/DCIM/Camera/EMOJI_2-7-116_22538.jpg:open failed:EACCES(Permission denied)

这是捕获屏幕的方法。

private void captureScreen() {
        Date now = new Date();
        now.getYear();
        now.getMonth();
        now.getDay();

        View v = findViewById(R.id.rl);
        v.setDrawingCacheEnabled(true);
        Bitmap bitmap = v.getDrawingCache();
        String dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                + File.separator + "Camera" + File.separator + "EMOJI_" +
                now.getDay() + "-" + now.getMonth() + "-" + now.getYear() + "_" + now.getHours() + now.getMinutes() + now.getSeconds() + ".jpg";
        File file = new File(dest);
        try {
            FileOutputStream stream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();
            Toast.makeText(getApplicationContext(), "Saved !", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), "An error occured", Toast.LENGTH_LONG).show();
        } finally {
            v.setDrawingCacheEnabled(false);
        }

        // Scan the image to make it appear in gallery
        MediaScannerConnection.scanFile(this, new String[]{file.getPath()}, new String[]{"image/jpeg"}, null);
    }

帮我解决一下这个问题?

3 个答案:

答案 0 :(得分:1)

如果targetSdkVersion为23或更高,则还需要request the WRITE_EXTERNAL_STORAGE permission at runtime

答案 1 :(得分:0)

您正在尝试访问系统上当前不存在的文件。

而是使用..

OutputStream out = new FileoutputStream(dest);

//这次使用String Object而不是File Object

实例化FileOutputStream对象

答案 2 :(得分:0)

尝试这种方式

File sd = Environment.getExternalStorageDirectory();
File image = new File(sd+filePath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);

可能对你有所帮助。