2013-12-17 67 views
1
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"); 
     } 
    }; 

使用此代码我可以存储到设备存储中。无法在Android上写入外部SD卡

DeviceStorage /仿真/ 0/camtest

我想它存储到外置SD卡。我如何做到这一点

我有以下权限

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

确保设备在测试时不通过USB进行根。 – Hank

+1

嗨@Hank没有我的设备没有根植 – user2626445

+0

它记录写过的字节行吗? – superfell

回答

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

即使使用您的代码,它仍然保存到手机的目录中。我也尝试重新启动我的设备[email protected] – user2626445

+0

@Hank--我已经取消注释,看到消息并说“我们可以读写”。拍完照片后,退出应用程序 – user2626445

+0

哦,可能是因为我已经完成了它,退出了我的脸 – user2626445