Android:Problem with camera(take pictures)
I have an application which makes use of the default camera to take pictures and save it in a DB.
The camera code that I've used is :
package com.android.SamsungMIv4;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
public class camera extends Activity {
private static final int TAKEPICTURE_ACTIVITY = 0;
/** Called when the activity is first c开发者_C百科reated. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
try{
startActivityForResult(i, TAKEPICTURE_ACTIVITY);
}catch(ActivityNotFoundException e){
Toast.makeText(this, "Application not available",
Toast.LENGTH_SHORT).show();}
//Log.e(TAG, "Error in taking picture");
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == TAKEPICTURE_ACTIVITY){
if (resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data") ;
}
}
}
}
But, the problem is that I cannot specify the name for the image captured and this will cause problems in DB for me. So, how do i 1) include a way to add the file name for the image captured OR 2) I'm assuming that 'bitmap' over here contains my captured image. If so, how do i save it to a location along with the name i specify. OR 3) As, this camera is a service I'm making use of in a separate activity, is there a way of returning the captured image to the previous activity using content values?
Obtaining the data, as you have done with the .getExtras("data") method, will allow you to obtain the high res version of the photo. To save the file check out the compress method[1]. Then using a FileOutputSteam[2] you can easily define the file name you want to save to.
http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)
http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileOutputStream.html
精彩评论