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

时间:2015-02-26 11:58:30

标签: android camera

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

@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();
}

无法正常工作

3 个答案:

答案 0 :(得分:7)

    @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"/>

答案 1 :(得分:0)

您可以从以下代码中找到

@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);
}
}

希望此代码可以帮助您

答案 2 :(得分:0)

首先必须创建要将文件保存到的目标文件夹。因此,在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;
    }