Posted by Shoaib at 10:36:00 am
Read our previous post
MainActivity.Java:
ackage com.example.shoaibm.camerawork; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import java.io.File; import java.io.IOException; public class MainActivity extends ActionBarActivity { int TAKE_PHOTO_CODE = 0; public static int count=0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //here,we are making a folder named picFolder to store pics taken by the camera using this application final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; File newdir = new File(dir); newdir.mkdirs(); Button capture = (Button) findViewById(R.id.button); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // here,counter will be incremented each time,and the picture taken by camera will be stored as 1.jpg,2.jpg and likewise. count++; String file = dir+count+".jpg"; File newfile = new File(file); try { newfile.createNewFile(); } catch (IOException e) {} Uri outputFileUri = Uri.fromFile(newfile); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } } }
Layout/ActivityMain.xml
//schemas.android.com/apk/res/android"
Manifest.xml
<uses-permission android:name="android.permission.CAMERA"></uses-permission> <uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"></uses-permission>
No comments:
Post a Comment