无法在Android中写入外部SD卡

时间:2013-12-17 01:00:33

标签: android file-io storage android-sdcard

PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;
            try {
                // Write to SD Card
                fileName = String.format("%d.jpg", System.currentTimeMillis());
                File file = new File(Environment.getExternalStorageDirectory().getPath()+"/camtest");
                if(!file.exists())
                    file.mkdirs();
                File outputFile = new File(file, fileName);
                outStream = new FileOutputStream(outputFile);
                outStream.write(data);
                outStream.close();
                Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);

                resetCam();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            Log.d(TAG, "onPictureTaken - jpeg");
        }
    };

使用此代码,我可以存储到设备存储中。

DeviceStorage /仿真/ 0 / camtest

我想将它存储在外部SD卡上。我该怎么做

我有以下许可

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

1 个答案:

答案 0 :(得分:0)

这是我在一个类中使用的代码,用于拍照并将其保存到我的SD卡上的文件夹中

makeOutPutFolder();

    Button capture = (Button) findViewById(R.id.capture);
    capture.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.v(DEBUG_TAG, "Requesting capture");
            cameraView.capture(new Camera.PictureCallback() {
                public void onPictureTaken(byte[] data, Camera camera) {
                    Log.v("Still", "Image data received from camera");
                    try {
                        File exportDir = new File(Environment.getExternalStorageDirectory(), "student_images");       
                        if (!exportDir.exists())
                        {
                            exportDir.mkdirs();
                        }
                        Log.d("Still image filename:", "capture.jpg");
                        File file = new File(exportDir + File.separator + STILL_IMAGE_FILE);
                         FileOutputStream WriteStudentImage = new FileOutputStream(file);
                       WriteStudentImage.write(data);
                       WriteStudentImage.close();
                       Toast.makeText(getApplicationContext(), "Finished Saving Image", Toast.LENGTH_LONG).show();
                       //updateImageCount(currentAnimal);
                       finish();
                    } catch (Exception e) {
                        Log.e("Still", "Error writing file", e);
                    }
                }
            });
        }
    });
 }

public void makeOutPutFolder(){
    String state = Environment.getExternalStorageState();

            if (Environment.MEDIA_MOUNTED.equals(state)){
                //We can read and write the media
                mExternalStorageAvailable = mExternalStorageWriteable = true;
                 //             Toast.makeText(getApplicationContext(), "We Can Read And Write ", Toast.LENGTH_LONG).show();
                  File file = new File(Environment.getExternalStorageDirectory()
                         +File.separator
                         +"student_images"); //folder name
                         file.mkdir();
            } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
                  mExternalStorageAvailable = true;
                  mExternalStorageWriteable = false;
              //                  Toast.makeText(getApplicationContext(), "We Can Read but Not Write ", Toast.LENGTH_LONG).show();
              }else{
                    //something else is wrong
                    mExternalStorageAvailable = mExternalStorageWriteable = false;
                     //                 Toast.makeText(getApplicationContext(), "We Can't Read OR Write ", Toast.LENGTH_LONG).show();
              }
}