我需要将图像保存到手机中的自定义文件夹中。
我的代码是

@Override
        public void onPictureTaken(byte[] data, Camera camera) {
            if (data != null) {
FileOutputStream outStream = null;
                try {
                    Random generator = new Random();
                    int n = 0000;
                    n = generator.nextInt(n);
                    String fName = "Image" + n + ".jpg";
                    outStream = new FileOutputStream(String.format(fName));
                    outStream.write(data);
                    outStream.close();
                } catch (Exception e) {
                  e.printStackTrace();
                }
                camera.startPreview();
}

不工作

最佳答案

    @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                if (data != null) {
    Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);

    if(bitmap!=null){

                        File file=new File(Environment.getExternalStorageDirectory()+"/dirr");
                        if(!file.isDirectory()){
                            file.mkdir();
                        }

                        file=new File(Environment.getExternalStorageDirectory()+"/dirr",System.currentTimeMillis()+".jpg");


                        try
                        {
                            FileOutputStream fileOutputStream=new FileOutputStream(file);
                            bitmap.compress(Bitmap.CompressFormat.JPEG,100, fileOutputStream);

                            fileOutputStream.flush();
                            fileOutputStream.close();
                        }
                        catch(IOException e){
                            e.printStackTrace();
                        }
                        catch(Exception exception)
                        {
                            exception.printStackTrace();
                        }

                    }
    }
}


Dont forget to add permission:

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

关于android - 将图像保存到 onPictureTaken 上的自定义文件夹 android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28741878/

10-10 04:19