簡體   English   中英

截屏並保存到應用程序自己的目錄中

[英]Taking screenshot and saving into app's own directory

我知道這個問題已經回答了,但是我想得到更好的解釋,因為我嘗試實現了它,但是它似乎沒有用。

我有以下代碼:

private void takeScreenshot() {

        ContextWrapper cw = new ContextWrapper(getApplicationContext());

        //Get screenshot
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        Date fileName = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", fileName);


        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        File image = new File(directory,fileName+".jpg");
        try {
            FileOutputStream fos = new FileOutputStream(image);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

我想做的是截取屏幕截圖,將其保存到具有我的應用程序名稱的文件夾中,並讓Android手機的圖庫可以讀取它。 我的代碼沒有執行以上任何操作。 使用文件資源管理器時,看不到帶有應用程序名稱的文件夾,該文件夾也未出現在圖庫中。 似乎它甚至不保存圖像。 您能告訴我代碼有什么問題嗎?

下面的代碼創建一個名為“ AppName”的目錄,然后將屏幕快照存儲在該目錄中。 畫廊也可以閱讀。 如果您沒有WRITE_EXTERNAL_STORAGE權限,則您的代碼(以及下面的代碼)將無法工作。

        private static File getOutputMediaFile() {
            // To be safe, you should check that the SDCard is mounted
            // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp"); //change to your app name

        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_" + timeStamp + ".jpg");


        return mediaFile;
    }

    private void takeScreenshot(){


    //Get screenshot
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);




    File pictureFile = getOutputMediaFile();


    if (pictureFile == null){


        Log.d(TAG, "error creating media file, check storage permission");

        return;
    }

    try {
        FileOutputStream fos = new FileOutputStream(pictureFile); 
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        bitmap.recycle();
    } catch (FileNotFoundException e) {

        Log.d(TAG, "File not found" + e.getMessage());
    } catch (Exception e) {

        e.printStackTrace();
    }

}

確保添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

到清單上,並要求在運行時授予權限

ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    00);

該代碼在getOutputMediaFile()方法中查找和/或創建一個具有應用程序名稱的目錄,然后在該目錄中返回一個以時間戳為名稱的文件。 然后在takeScreenshot()方法中,將屏幕快照bitmap轉換為byte[]並使用fileOutputStream將此byte[]寫入getOutputMediaFile()返回的文件。

結果是將屏幕截圖保存到目錄“ MyCameraApp”中的畫廊(更改為您的應用名稱)

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM