无法解决方法 ‘getExternalStoragePublicDirectory’。

huangapple go评论88阅读模式
英文:

cannot resolve method 'getExternalStoragePublicDirectory'

问题

我尝试保存捕获的图像但出现了类似标题的错误

private File createPhotoFile(){
        File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/sidentanpic/");
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(storageDir)));
        try{
            if(storageDir.exists()){
                System.out.println("Folder tersedia");
            }else{
                storageDir.mkdir();
                System.out.println("Folder telah dibuat");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        String name = new SimpleDateFormat("yyyyMMddd_HHmmss").format(new Date());
        File image =null;
        try{
            image = File.createTempFile(name,".jpg", storageDir);
        }catch(Exception e){
            Log.d("Failed","Gagal menyimpan "+ e.toString());
        }
        return image;
}

请问我是否需要在AndroidManifest中添加一些内容?

英文:

I try to save the captured image but got an error like the title.

private File createPhotoFile(){
        File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES +"/sidentanpic/");
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(storageDir)));
        try{
            if(storageDir.exists()){
                System.out.println("Folder tersedia");
            }else{
                storageDir.mkdir();
                System.out.println("Folder telah dibuat");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        String name = new SimpleDateFormat("yyyyMMddd_HHmmss").format(new Date());
        File image =null;
        try{
            image = File.createTempFile(name,".jpg", storageDir);
        }catch(Exception e){
            Log.d("Failed","Gagal menyimpan "+ e.toString());
        }
        return image;
    }

should I add something in AndroidManifest?

答案1

得分: 1

getExternalStoragePublicDirectory()Environment类中的静态方法。因此,请尝试如下:

Environment.getExternalStoragePublicDirectory()

但是,在Android Q版本中,此方法已被弃用。有关更多信息,请查阅文档

英文:

getExternalStoragePublicDirectory() is static method in Environment class. So, please try as below:

Environment.getExternalStoragePublicDirectory()

But, This method is deprecated in Android Q version. Please check the documentation for more info on this.

huangapple
  • 本文由 发表于 2020年7月26日 21:25:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63100749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定