【问题标题】:Unable to write on to the External SD card in Android无法在 Android 中写入外部 SD 卡
【发布时间】:2013-12-17 01:00:32
【问题描述】:
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/Emulated/0/camtest

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

我有以下权限

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

【问题讨论】:

  • 在测试时确保设备没有通过 USB 植根。
  • 嗨@Hank 不,我的设备没有root
  • 它是否记录 writeBytes 行?
  • 是的@superfell 它确实也记录了 writeBytes 行的大小
  • @superfell 我也可以访问从文件管理器写入的文件

标签: android file-io storage android-sdcard


【解决方案1】:

这是我在课堂上的代码,用于拍照并将其保存到我的 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();
              }
}

【讨论】:

  • 即使使用您的代码,它仍会保存到手机目录中。我也尝试重新启动我的设备。---@Hank
  • @Hank-- 我已取消注释 toast 以查看消息,并显示“我们可以读写”。拍照后退出应用程序
  • 哦可能是因为我已经完成它退出我的脸
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 1970-01-01
相关资源
最近更新 更多