2011-12-21 7 views
0
outStream = new FileOutputStream(String.format(
         "/sdcard/%d.jpeg", System.currentTimeMillis())); 

カメラから画像を電話に保存しようとしています。私は写真を撮ることができます。SurfaceViewの写真を見てください。私の電話のファイルシステムに行くと、写真はどこにも見つかりません。私はそれがこの特定のコード行だと思っていますが、確実に私の活動全体を投稿します。このコード行で何が問題になっていますか?

package com.commonsware.android.skeleton; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.hardware.Camera; 
import android.hardware.Camera.*; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.view.Display; 
import android.view.Surface; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.FrameLayout; 

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.List; 

// ---------------------------------------------------------------------- 

public class SimpleBulbActivity extends Activity { 
    private Preview mPreview; 
    private static final String TAG = "CameraDemo"; 
    FrameLayout preview; 
    Camera mCamera; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Hide the window title. 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.main); 
    } 

    protected void onResume() { 
     super.onResume(); 
     //Setup the FrameLayout with the Camera Preview Screen 
     mPreview = new Preview(this); 
     preview = (FrameLayout)findViewById(R.id.preview); 
     preview.addView(mPreview); 
    } 

    public void snap(View view) { 
     mCamera.takePicture(shutterCallback, rawCallback, jpegCallback); 
    } 
    ShutterCallback shutterCallback = new ShutterCallback() { 
     public void onShutter() { 
      Log.d(TAG, "onShutter'd"); 
     } 
    }; 

    PictureCallback rawCallback = new PictureCallback() { 
     public void onPictureTaken(byte[] _data, Camera _camera) { 
      Log.d(TAG, "onPictureTaken - raw"); 
     } 
    }; 

    PictureCallback jpegCallback = new PictureCallback() { 
     public void onPictureTaken(byte[] data, Camera _camera) { 
      FileOutputStream outStream = null; 
      try { 
       // write to local sandbox file system 
       // outStream = 
       // CameraDemo.this.openFileOutput(String.format("%d.jpg", 
       // System.currentTimeMillis()), 0); 
       // Or write to sdcard 
       outStream = new FileOutputStream(String.format(
         "/sdcard/%d.jpeg", System.currentTimeMillis())); 
       sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
       outStream.write(data); 
       outStream.close(); 
       Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
      } 
      Log.d(TAG, "onPictureTaken - jpeg"); 
     } 
    }; 

// ---------------------------------------------------------------------- 

    class Preview extends SurfaceView implements SurfaceHolder.Callback { 
     SurfaceHolder mHolder; 

     Preview(Context context) { 
      super(context); 

      // Install a SurfaceHolder.Callback so we get notified when the 
      // underlying surface is created and destroyed. 
      mHolder = getHolder(); 
      mHolder.addCallback(this); 
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     } 

     public void surfaceCreated(SurfaceHolder holder) { 
      // The Surface has been created, acquire the camera and tell it where 
      // to draw. 
      mCamera = Camera.open(); 
      try { 
       mCamera.setPreviewDisplay(holder); 
       mCamera.setPreviewCallback(new PreviewCallback() { 

       public void onPreviewFrame(byte[] data, Camera arg1) { 
        FileOutputStream outStream = null; 
        try { 
         outStream = new FileOutputStream(Environment.getExternalStorageDirectory().toString()); 
         outStream.write(data); 
         outStream.close(); 
         Log.d(TAG, "onPreviewFrame - wrote bytes: " 
           + data.length); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } finally { 
        } 
        Preview.this.invalidate(); 
       } 
      }); 
     } catch (IOException e) { 
       mCamera.release(); 
       mCamera = null; 
       e.printStackTrace(); 
      } 
     } 

     public void surfaceDestroyed(SurfaceHolder holder) { 
      // Surface will be destroyed when we return, so stop the preview. 
      // Because the CameraDevice object is not a shared resource, it's very 
      // important to release it when the activity is paused. 
      mCamera.stopPreview(); 
      mCamera.release(); 
      mCamera = null; 
     } 


     private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) { 
      final double ASPECT_TOLERANCE = 0.05; 
      double targetRatio = (double) w/h; 
      if (sizes == null) return null; 

      Size optimalSize = null; 
      double minDiff = Double.MAX_VALUE; 

      int targetHeight = h; 

      // Try to find an size match aspect ratio and size 
      for (Size size : sizes) { 
       double ratio = (double) size.width/size.height; 
       if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; 
       if (Math.abs(size.height - targetHeight) < minDiff) { 
        optimalSize = size; 
        minDiff = Math.abs(size.height - targetHeight); 
       } 
      } 

      // Cannot find the one match the aspect ratio, ignore the requirement 
      if (optimalSize == null) { 
       minDiff = Double.MAX_VALUE; 
       for (Size size : sizes) { 
        if (Math.abs(size.height - targetHeight) < minDiff) { 
         optimalSize = size; 
         minDiff = Math.abs(size.height - targetHeight); 
        } 
       } 
      } 
      return optimalSize; 
     } 

     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
      // Now that the size is known, set up the camera parameters and begin 
      // the preview. 
      Camera.Parameters parameters = mCamera.getParameters(); 

      List<Size> sizes = parameters.getSupportedPreviewSizes(); 
      Size optimalSize = getOptimalPreviewSize(sizes, w, h); 

      Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 

      if(display.getRotation() == Surface.ROTATION_0) 
      { 
       parameters.setPreviewSize(optimalSize.height, optimalSize.width);       
       mCamera.setDisplayOrientation(90); 
      } 

      if(display.getRotation() == Surface.ROTATION_90) 
      { 
       parameters.setPreviewSize(optimalSize.width, optimalSize.height);       
      } 

      if(display.getRotation() == Surface.ROTATION_180) 
      { 
       parameters.setPreviewSize(optimalSize.width, optimalSize.height);    
      } 

      if(display.getRotation() == Surface.ROTATION_270) 
      { 
       parameters.setPreviewSize(optimalSize.width, optimalSize.height); 
       mCamera.setDisplayOrientation(0); 
      } 

      mCamera.setParameters(parameters); 
      mCamera.startPreview(); 
     } 

    } 

} 
+4

* "このコード行には何が問題なのですか?" * - これは、sdcardへのパスをハードコードします。これはデバイスによって異なるため、これは悪いことです。代わりに['Environment.getExternalStorageDirectory()'](http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29)を使ってパスを決定してください。これが正確な問題であるかどうかは分かりませんが、一般的なアドバイスだけです。 –

+4

'私はそれがこの特定のコード行だと思っていますが、私は自分のアクティビティ全体を投稿するだけです。'あなたは、コードのどの行を推測するのではなく問題にするのか本当に時間を費やすべきです。 –

+1

私はAndroidが初めてです。私が確実にそれを行うことができたら、私はそうするでしょう。しかし、私は確かに、これでもまだ悪いです。だから私はあなたに私がSUSPECTの犯人である線を与えましたが、私は今確かに知る方法を持っています。私は本当にalextscによって与えられたアドバイスを感謝します。私はこれを試してみる。 –

答えて

1

はあなたのAndroidManifest.xmlで

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

を持っているのですか?

+0

私はそれを持っていなかった、私はそれを完全に忘れてしまった。ありがとう!しかし、まだ画像を保存していません。 –

+0

これは、 'Environment.getExternalStorageDirectory()'と一緒に問題を修正しました! –

関連する問題