簡體   English   中英

圖庫不會顯示已保存的圖像

[英]Gallery won't show images saved

我已經實現了一個代碼,其中屏幕快照被保存到Pictures目錄下的文件夾中。 畫廊為什么不顯示圖像?

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), "Solitude"); //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("ss", "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("ss", "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("ss", "File not found" + e.getMessage());
        }
        catch (IOException e) {
            Log.d("ss", "Error Accessing File" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

忽略此消息跳過此愚蠢的提示,要求我添加更多詳細信息,因為我的帖子主要是代碼。 忽略此消息。

保存圖像后,您將需要掃描文件,以便它可以顯示在圖庫中。

MediaScannerConnection.scanFile(context, new String[]{mediaFile.getAbsolutePath()},
    new String[]{"image/jpeg"}, null);

暫無
暫無

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

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