2012-04-03 60 views
1

在我的应用程序中,当用户按下相机视图时,我需要保存相机的图片。到目前为止,我遵循官方教程:http://developer.android.com/guide/topics/media/camera.html,除了当我尝试在回调中保存图片时,一切正常。事实上,我在try/catch块中没有发现错误,但我的文件无处可查。将相机图片保存在文件中,未创建文件

我有添加正确的权限,可以在mannifest:

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

在我的回调,我有:

private PictureCallback mPicture = new PictureCallback() 
    { 
     public void onPictureTaken(byte[] data, Camera camera) 
     { 
      Log.d("tag", "Je rentre bien dans la callback"); 

      //File pictureFile = media.getOutputMediaFile(MediaFile.MEDIA_TYPE_IMAGE); 
      File pictureFile = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), "temp.png"); 


      if (pictureFile == null) 
      { 
       Log.d("tag", "Error creating media file, check storage permissions "); 
       return; 
       } 

      try 
      { 
       FileOutputStream fos = new FileOutputStream(pictureFile); 
       Log.d("tag", "Fichier créer : " + pictureFile.getAbsolutePath()); 
       fos.write(data); 
       fos.close(); 
      } 
      catch (FileNotFoundException e) 
      { 
       Log.d("tag", "File not found " + pictureFile.getAbsolutePath() 
         + " Readable : " + pictureFile.canRead() 
         + " Writeable : " + pictureFile.canWrite() 
         + " Exist : " + pictureFile.exists()); 
      } 
      catch (IOException e) 
      { 
       Log.d("tag", "Error accessing file"); 
      } 
     } 

我的日志文件中显示我的这个:

04-03 10:29:28.950: D/tag(15087): Je rentre bien dans la callback 
    04-03 10:29:28.960: D/tag(15087): Fichier créer : /mnt/sdcard/Pictures/temp.png 

我检查平板电脑上的SD卡,但就像我说的文件无处可查。我研究我的问题,但我还没有发现任何有用的东西。 我没有看到我的代码出了什么问题或缺少什么。

我希望这里有人能帮助我。

回答

0

试试这个东西。

File path = new File(Environment.getExternalStorageDirectory()+ getResources().getString(R.string.QRCode_Path)); 
    path.mkdirs(); 

主要的是path.mkdirs();它会创建即使没有创建一个文件夹,然后文件将被保存。

+0

我试了一下,并没有改变,事情该文件仍然保存无门。它甚至没有创建目录。当我进行保存时,我处于一个片段中,我不知道它是否会改变某些内容。我编辑我的日志说: 04-03 15:48:29.000:D/tag(18524):Fichiercréer/mnt/sdcard/Pictures/temp.png可读:true可写:true存在:true – JT117 2012-04-03 15:51:55

2

你可以尝试这样的

System.gc(); 
String fileName = System.currentTimeMillis()+".jpg"; //you can give any name to your image file 
String mPathImage = Environment.getExternalStorageDirectory()+ "/" + fileName; 
File file = new File(mPathImage); 
Uri outputFileUri = Uri.fromFile(file); 
Intent mIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
mIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
startActivityForResult(mIntent, 1); 
相关问题