2014-01-18 293 views
0

我已經嘗試了很多相機示例爲Android,但thishttp://androideity.com/2011/10/08/hello-camera/)是冷杉我得到保存圖像並在畫廊上查看它。但我想知道的是如何更改保存圖像的文件夾。我想實際上它可以將它們保存在sdcard0/DCIM之上。更改目錄文件 - 安卓相機

下面是主要活動:

public class Foto extends Activity implements SurfaceHolder.Callback{ 

private LayoutInflater myInflater = null; 
Camera myCamera; 
byte[] tempdata; 
boolean myPreviewRunning = false; 
private SurfaceHolder mySurfaceHolder; 
private SurfaceView mySurfaceView; 
ImageButton takePicture; 

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

    getWindow().setFormat(PixelFormat.TRANSLUCENT); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    setContentView(R.layout.foto); 

    mySurfaceView = (SurfaceView) findViewById(R.id.surface); 
    mySurfaceHolder = mySurfaceView.getHolder(); 
    mySurfaceHolder.addCallback(this); 
    mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    myInflater = LayoutInflater.from(this); 
    View overView = myInflater.inflate(R.layout.segundacapa,null); 
    this.addContentView(overView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 

    takePicture = (ImageButton) findViewById(R.id.button); 
    takePicture.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      myCamera.takePicture(myShutterCallback, myPictureCallback, myJpeg); 
     } 
    }); 


} 

ShutterCallback myShutterCallback = new ShutterCallback() { 
    @Override 
    public void onShutter() { 
    } 
}; 

PictureCallback myPictureCallback = new PictureCallback() { 
    @Override 
    public void onPictureTaken(byte[] data, Camera myCamera) { 
     // TODO Auto-generated method stub 
    } 
}; 

PictureCallback myJpeg = new PictureCallback() { 
    @Override 
    public void onPictureTaken(byte[] data, Camera myCamera) { 
     // TODO Auto-generated method stub 
     if(data != null){ 
      tempdata = data; 
      done(); 
     } 
    } 
}; 

void done(){ 
    Bitmap bm = BitmapFactory.decodeByteArray(tempdata, 0, tempdata.length); 
    String url = Images.Media.insertImage(getContentResolver(), bm, null, null); 
    bm.recycle(); 
    Bundle bundle = new Bundle(); 
    if(url != null){ 
     bundle.putString("url",url); 
     Intent mIntent = new Intent(); 
     mIntent.putExtras(bundle); 
     setResult(RESULT_OK, mIntent); 
     Toast.makeText(this, "Picture saved on" + url, Toast.LENGTH_SHORT).show(); 
    } 
    else{ 
     Toast.makeText(this, "Picture can not be saved", Toast.LENGTH_SHORT).show(); 
    } 
    finish(); 
} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
    // TODO Auto-generated method stub 
    try{ 
     if(myPreviewRunning){ 
      myCamera.stopPreview(); 
      myPreviewRunning = false; 
     } 
     Camera.Parameters p = myCamera.getParameters(); 
     p.setPreviewSize(width,height); 

     myCamera.setParameters(p); 
     myCamera.setPreviewDisplay(holder); 
     myCamera.startPreview(); 
     myPreviewRunning = true; 
    }catch(Exception e){} 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    // TODO Auto-generated method stub 
     myCamera = Camera.open(); 
    } 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    // TODO Auto-generated method stub 
    myCamera.stopPreview(); 
    myPreviewRunning = false; 
    myCamera.release(); 
    myCamera = null; 
    }  
} 

還有與SurfaceView(稱爲foto.xml),另一個稱爲segundacapa.xml與按鈕的XML佈局。

非常感謝!

回答

1

而不是使用Images.Media在磁盤上存儲的圖像(使用默認位置),您可以使用insertImage()另一種味道,這需要文件路徑。你應該先寫文件。您不需要將onPictureTaken()->data轉換爲Bitmap;您只需將byte[]寫入Jpeg文件即可。

FileOutputStream fos = openFileOutput("saved.jpg", Context.MODE_PRIVATE); 
fos.write(data); 
fos.close(); 

您可以根據需要選擇文件名;你可以保存與其他參數(見更多Context.openFileOutput()文件可以打開詭計上/SD卡,如果你願意的話,如

FileOutputStream fos = new FileOutputStream("/sdcard/saved.jpg"); 

請注意,您需要申請許可寫入SD卡。檢查顯示以下行AndroidManifest.xml中

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