How to use android Device native camera..? - android

I am developing an app where i want to use device native camera.i.e. i want to use the exact camera which the device will show when we click on camera option from device including widgets like front camera option etc. i saw one app in android market which exactly doing this. Can any one help me??
Thanks,
Ram.

If u want to save the image in a folder, then this is the easiest way:
try {
//where to save the image
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "foldername" + File.separator);
root.mkdirs();
sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
startCameraActivity();
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
finish();
}
protected void startCameraActivity() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 101);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==101 && resultCode==-1)
{
try
{
//use that file here
}
catch(Exception ex)
{
Log.v("OnCameraCallBack",ex.getMessage());
}
}
If u want to save it in default location:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 101);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==101 && resultCode==-1)
{
try
{
//use that file here
}
catch(Exception ex)
{
Log.v("OnCameraCallBack",ex.getMessage());
}
}

Related

Open a default folder path in Android?

Issue:
Office is a folder in internal memory of Android.
Clicking a button in screen should always take one to default folder, Office.
Appreciate help as no accepted answers found.
In onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null)
return;
if (requestCode == viewfilerequestcode){
Uri Fileuri = data.getData();
String DFLocation="/mnt/sdcard/Office/";
String FilePath="";
if (FilePath.trim().equals(DFLocation.concat(GettheFilename(Fileuri))))
{
FilePath = DFLocation.concat(GettheFilename(Fileuri));
}
......
.......
........
}
.........
..........
}
Intent start:
btnview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent fileintent;
String[] mimestoview =
{"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx };
fileintent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
fileintent.setType("*/*");
fileintent.putExtra(Intent.EXTRA_MIME_TYPES, mimestoview );
fileintent.addCategory(Intent.CATEGORY_OPENABLE);
fileintent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
fileintent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivityForResult(fileintent, viewfilerequestcode);
} catch (ActivityNotFoundException e) {
lbl.setText("No activity to handle picking a file. Showing alternatives.");
}
}
});

SAF - ACTION_CREATE_DOCUMENT - after I save the file on Drive the file is empty

I am a beginner in SAF. What I'm trying to do is super simple to save a config. Let's say the file is .conf.
I copy .conf to conf.txt and I save it on Drive.
Here is my code:
tools.deleteFile(dst); // delete conf.txt if it exists
int res = tools.copyFile(src,dst); // copy .conf to conf.txt
if(res == -1) return;
tools.viewFile(dst);
// verify in Log info that the content of cnf.txt is correct
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, dst);
startActivity(intent);
I do a save in Drive. The file appears on my pc but when I open it, it's empty.
When I do the inverse: ACTION_OPEN_DOCUMENT
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
Uri uri;
if (resultCode == Activity.RESULT_OK){
if (requestCode == 30){
if (resultData != null) {
uri = resultData.getData();
try {
String content =
readFile(uri);
} catch (IOException e) {
e.printStackTrace();
}
The function readFile opens the file and stops while reading because there is no data.
What did I do wrong?
The Intent(Intent.ACTION_CREATE_DOCUMENT) is for CREATING text file, and use onActivityResult() to get the uri (location) from the file, THEN you use OutputStream to WRITE data (byte[]) to the file.
private void createAndSaveFile() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, "testFileSam.txt");
startActivityForResult(intent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
try {
Uri uri = data.getData();
OutputStream outputStream = getContentResolver().openOutputStream(uri);
outputStream.write("Hi, welcome to Sam's Android classroom! Have a good day!".getBytes());
outputStream.close();
Toast.makeText(this, "Write file successfully", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Fail to write file", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "File not saved", Toast.LENGTH_SHORT).show();
}
}
}
You are just creating the document but not writing it.
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TITLE, dst);
startActivity(intent);
this will create document and return the uri of the document to your app.
Now you need to write something to this uri that you will get in onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
try {
Uri uri = data.getData();
OutputStream outputStream = getContentResolver().openOutputStream(uri);
outputStream.write("Hi, welcome to Sam's Android classroom! Have a good day!".getBytes());
outputStream.close(); // very important
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

How can I get the URI with a custom camera in Android?

I'm switching from using the default camera to finally growing a pair and deciding to make a custom camera. It's only showing me how much I don't fully understand.
Here is basically way I have been doing things in the main activity as far as photos go, but it will no longer work:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_PHOTO_REQUEST) {
if (data == null) {
Toast.makeText(this, "General Error!", Toast.LENGTH_LONG).show();
}
else {
mMediaUri = data.getData();
}
Log.i(TAG, "Media URI: " + mMediaUri);
}
else {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(mMediaUri);
sendBroadcast(mediaScanIntent);
}
(...)
Here is the picture saving function along with a couple others of the new camera:
(...)
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Picture taken");
String path = savePictureToFileSystem(data);
setResult(path);
finish();
}
private static String savePictureToFileSystem(byte[] data) {
File file = getOutputMediaFile();
saveToFile(data, file);
return file.getAbsolutePath();
}
private void setResult(String path) {
Intent intent = new Intent();
intent.putExtra(EXTRA_IMAGE_PATH, path);
setResult(RESULT_OK, intent);
}
*Credit to Paul Blundell
(...)
What do I need to do so that the main activity can receive the image's URI in the onactivityresult instead of the path String? Are URIs even applicable when it comes to custom cameras?
Please and thanks.
You use custom camera, but want to do it via Intent? OK, you have the absolute file path in
String abspath = data.getExtras().getString(EXTRA_IMAGE_PATH);
mMediaUri = Uri.fromFile(new File(abspath));

How to process a photo taken by an intent?

I'm completely news on android thing and unfortunately with little few time to learn it by the right way, I have a work to release.
The problem is: I need to take a picture and process her with an algorithm that I made.
I did it by the easiest way that I could find, I know it looks like really trahsie for those who really get android (sorry)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
takePic();
protected void takePic(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 100);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle extras = data.getExtras();
mImageBitmap = (Bitmap) extras.get("data");
Algorithm(mImageBitmap)
But it doesn't process, it takes a photo, ask to save or cancell and leaves the application, I have already made by different ways (creating a new activity), but nothing seems to work
Heres how I did it
To go to the camera:
Somewhere, declaire a fileUri variable and hold onto it
Uri fileUri;
final int TAKE_PICTURE=100;//this can be any int, really
public void goToCamera(){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo;
try
{
// place where to store camera taken picture
photo = this.createTemporaryFile("picture", ".jpg");
Log.v(TAG, "Here(after createTempFile)");
photo.delete();
}
catch(Exception e)
{
Log.v(TAG, "Can't create file to take picture!" + e.getMessage());
Toast.makeText(context, "Please check SD card!", Toast.LENGTH_SHORT).show();
return;
}
fileUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
//Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE);
}
Then to retreive the image
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK){
this.getContentResolver().notifyChange(uri, null);
ContentResolver cr = this.getContentResolver();
Bitmap bitmap;
try
{
BitmapFactory.Options ops = new BitmapFactory.Options();
ops.inSampleSize = 4;
bitmap = BitmapFactory.decodeFile(uri.getPath().toString(), ops);
}
catch (Exception e)
{
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Failed to load", e);
}
}
}
The create temp file mentioned above:
private File createTemporaryFile(String part, String ext) throws Exception
{
File tempDir= Environment.getExternalStorageDirectory();
tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
Log.i(TAG, tempDir.toString());
if(!tempDir.exists())
{
Log.i(TAG, "Dir doesnt exist");
tempDir.mkdirs();
}
return File.createTempFile(part, ext, tempDir);
}
I realize this isn't probably as simple as you were hoping for, but this approach seemed to be as flexible and compatible as possible. Let me know if I left anything else out

Taking a photo by Intent on android

I am trying to take a photo by using an Intent.
My code looks like this and I don't know where the problem is.
When I want to get my app started, I get error:
12-20 06:09:03.243: E/AndroidRuntime(1048): java.lang.RuntimeException: com.android.camera.CameraHardwareException: java.lang.RuntimeException: Fail to connect to camera service
Try this :
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temp" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);
startCameraActivity();
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
protected void startCameraActivity() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 101);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==101 && resultCode==-1)
{
try
{
// "/mnt/sdcard/temp/myPicName.jpg" is ur image file if u want to use it
}
catch(Exception ex)
{
}
}
Intent for taking photo is as follows:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
Now in activity result you will get image capture path.
Hope this helps you

Categories

Resources