在 onPictureTaken 上将图像保存到自定义文件夹 android

Save image to custom folder android on onPictureTaken

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

@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 m_bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
String destinationPath = Environment.getExternalStorageDirectory() + "/" +    "Image.jpg";
if (m_bitmap != null || destinationPath != null)
{
FileOutputStream m_out = new FileOutputStream(destinationPath);
m_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, m_out);
}
}

希望这段代码对您有所帮助

您首先必须创建要将文件保存到的目标文件夹。因此,在您的 Android sdk 文件夹中,您创建了 MediaFiles 文件夹。然后您可以在此文件夹中创建文件。

您可以尝试以下代码。

@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(GetPathForFileName(fName)); 
                    outStream.write(data);
                    outStream.close();
                } catch (Exception e) {
                  e.printStackTrace();
                }
                camera.startPreview();
}

private String GetPathForFileName(String fileName)
    {
        try
        {
            String savePath = GetSaveFolderFromConfiguration();

            File file = new File(savePath + "/" + fileName);

            return savePath + "/" + fileName;
        }
        catch (Exception ex)
        {
            ExceptionForm.ShowException(ex);
        }
        return null;
    }

public static String GetSaveFolderFromConfiguration() throws Exception
    {
        String path = Environment.getExternalStorageDirectory().toString() + "/MediaFiles";
        File f = new File(path);
        if (f.exists())
            return path;
        else
            f.mkdir();
        return path;
    }
    @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"/>