onActivityResult data to inputstream chrashes on jellybean - android

This is code i'm using
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
InputStream is = getContentResolver().openInputStream(data.getData());
Main.this.getContentResolver().delete(data.getData(), null,
null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
String error = e.toString();
Dialog d = new Dialog(this);
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
} else {
is = null;
}
}
I'm doing this way because i don't want to save pics to dcim folder.
It is working fine on samsung, htc and some other devices but it crashes on alcatel one touch 5020x Jelly Bean 4.1.1,
returns null pointer exception.
Is there another way to do this, but not to save pics to dcim folder.
I have seen many solutions to do this but all of them save a pic to dcim folder
Thanx

I'm doing this way because i don't want to save pics to dcim folder.
Then include EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, to tell whichever camera app handles your request where to put the image. Quoting the documentation:
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field.
Your code is written to not do anything of what is documented. Instead, you are assuming that the camera app will return a Uri of where an image is. This is not part of the documented protocol, and so your code will fail when interacting with many camera apps.
Is there another way to do this, but not to save pics to dcim folder.
This code will put the image in another spot on external storage:
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
public class CameraContentDemoActivity extends Activity {
private static final int CONTENT_REQUEST=1337;
private File output=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
output=new File(getExternalFilesDir(null), "CameraContentDemo.jpeg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
// use the output File object
}
}
}
}

This is not the best solution but it works!
final static int cameraData = 13579;
final static int camera2Data = 97531;
InputStream is = null;
int camera2 = 0;
SharedPreferences spData;
public static String spName = "MySharedString";
camera start:
if (camera2 == 1) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
output = new File(getExternalFilesDir(null),
"CameraContentDemo.jpg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, camera2Data);
} else {
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
}
onActivitrForResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == cameraData) {
if (resultCode == RESULT_OK) {
try {
is = getContentResolver().openInputStream(data.getData());
Main.this.getContentResolver().delete(data.getData(), null,
null);
} catch (Exception e) {
// TODO Auto-generated catch block
camera2 = 1;
if (camera2 == 1) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
output = new File(getExternalFilesDir(null),
"CameraContentDemo.jpeg");
i.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(output));
startActivityForResult(i, camera2Data);
}
SharedPreferences.Editor editor = spData.edit();
editor.putInt("cam2", 1);
editor.commit();
}
} else {
is = null;
}
}
if (requestCode == camera2Data) {
if (resultCode == RESULT_OK) {
try {
is = new FileInputStream(output);
} catch (Exception e) {
// TODO Auto-generated catch block
SharedPreferences.Editor editor = spData.edit();
editor.putInt("cam2", 0);
editor.commit();
}
} else {
is = null;
}
}
}
So, like i said, its not the best solution but it works.
When the app starts for the first time on Alcatel, it starts with the old code, crashes and catcher the exception, saves camera2 variable to be 1, and starts the camera again with the second code that works fine.
When i load on other devices, it works fine to and no pics are saved to DCIM folder

Related

how to start camera intent and save a non-compressed picture

I am a very young self taught developer and I'm working on my first major project, which requires to start a camera intent once pressed, save the image that the user took and display it in a custom dialog.
I got it to work, but i stored the returned bitmap in onactivityresult so the picture is compressed and that destroys the functionality of the app.
HERE IS THE CODE THAT DOES WORK:
start intent:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
Recieve Intent and send data to dialog:
Bundle bundle = data.getExtras();
File file = new File(getCacheDir() + "/app"
+ System.currentTimeMillis() + ".jpg");
Bitmap bitmap = (Bitmap) bundle.get("data");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100 /* ignored for PNG */,
bos);
byte[] bitmapdata = bos.toByteArray();
// write the bytes in file
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos = new FileOutputStream(file);
fos.write(bitmapdata);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mdialog.setPic(file.getAbsolutePath());
Display the picture in the custom dialog:
public void setPic(final String mURi) {
this.mURI = mURi;
if (mURI != null) {
hwPic.postDelayed(new Runnable() {
#Override
public void run() {
Drawable d = Drawable.createFromPath(mURI);
hwPic.setImageDrawable(d);;
hwPic.setVisibility(View.VISIBLE);
}
}, 1000);
}
}
This works fine but since the picture is compressed any reasonably sized font in the picture is blury and illegible.
HERE IS THE CODE THAT DOES NOT WORK:
Initialize Variable:
private String MURID;
Start intent:
File file = new File(getCacheDir() + "/app"
+ System.currentTimeMillis() + ".jpg");
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
MURID=file.getAbsolutePath();
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT , Uri.parse(MURID));
startActivityForResult(intent, 1);
recieve intent and send to mydialog:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {// camera intent for the dialog picture
if (resultCode == RESULT_OK) {
mdialog.setPic(MURID);
}
}
}
setpic remains the same(in the dialog):
public void setPic(final String mURi) {
this.mURI = mURi;
if (mURI != null) {
hwPic.postDelayed(new Runnable() {
#Override
public void run() {
Drawable d = Drawable.createFromPath(mURI);
hwPic.setImageDrawable(d);;
hwPic.setVisibility(View.VISIBLE);
}
}, 1000);
}
}
Im not getting any response from it and logcat didnt give me any errors either, what seems to be the problem? any help would be greatly apprecieated.
BTW: i want this to work with phones without sdcards as well.
Third-party camera apps cannot write to your getCacheDir(), and some may get confused if you point to an existing file. Use external storage instead:
package com.commonsware.android.camcon;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
public class CameraContentDemoActivity extends Activity {
private static final int CONTENT_REQUEST=1337;
private File output=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output=new File(dir, "CameraContentDemo.jpeg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(output), "image/jpeg");
startActivity(i);
finish();
}
}
}
}
(from this sample project in this book)
BTW: i want this to work with phones without sdcards as well.
External storage is not removable storage.

Get uri from camera intent in android

on click
takePic.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent m_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageUri = getImageUri();
m_intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(m_intent, REQUEST_IMAGE_CAPTURE);
}
});
On Result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE
&& resultCode == RESULT_OK) {
Log.d("test1",""+imageUri);
Intent shareIntent = new Intent(this, SharePicForm.class);
shareIntent.putExtra("photo",""+imageUri);
startActivity(shareIntent);
}
}
getImageUri()
private Uri getImageUri(){
Uri m_imgUri = null;
File m_file;
try {
SimpleDateFormat m_sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
m_curentDateandTime = m_sdf.format(new Date());
m_imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + m_curentDateandTime + ".jpg";
m_file = new File(m_imagePath);
m_imgUri = Uri.fromFile(m_file);
} catch (Exception p_e) {
}
return m_imgUri;
}
What I would like to achieve is very simple, call camera intent and get the uri of the result photo. But it seems there is an inconsistent of different device and on my device it isn't work at all. I tried to store the path on a public variable but when I retrieve it , it is null, is there formal and standard way to implement it and should be work on all device? Also, are there any way for not provide the custom path but get the default uri path from the camera intent ?
Thanks for helping
If your device does not respect cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, you still can use
Uri imageUri = data.getData();
in onActivityResult(int requestCode, int resultCode, Intent data). But in most cases, the problem is that RAM is limited, and the system destroys your activity to give enough memory to the camera app to fulfill your intent. Therefore, when the result is returned, the fields of your activity are not initialized, and you should follow Amit's suggestion and implement onSavedInstance() and onRestoreInstanceState().
First of all make sure that your directory is created......
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ "/Folder/";
File newdir = new File(dir);
newdir.mkdirs();
on button click call this function.
private void capturarFoto() {
String file = dir+DateFormat.format("yyyy-MM-dd_hhmmss", new Date()).toString()+".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("Demo Pic", "Picture is saved");
}
}
Make sure You add permission in manifest
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
To just get the thumbnail, you can use this:
val imageBitmap = data.extras.get("data") as Bitmap
To get the full picture:
See the specific steps from here (official docs)
You can read more from the docs.
After a lot of research I found this solution. Just to make things clear I created an entire app for this question which serves the purpose of opening the camera clicking a photo and setting the image as the ImageBitmap of an ImageView. The code asked in this question starts at the second block i.e. the setView() method which is below the onCreate() method following which we have the onActivityResult() method
Here is a demo of the app.
Below I have Attached the MainActivity.java file
package com.cr7.opencamera;
import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private Button buttonCaptureImageFromCamera;
private Uri imageUri;
private ImageView imageViewCameraImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button that will open the camera
buttonCaptureImageFromCamera = findViewById(R.id.buttonCaptureImageFromCamera);
// ImageView that will store the image
imageViewCameraImage = findViewById(R.id.imageViewCameraImage);
askPermission();
}
This what you need for geting the Uri of the image here imageUri is a global variable so that it can be used in the onActivityResult() method
// Sets OnClickListener for the button if storage permission is given
private void setView() {
buttonCaptureImageFromCamera.setOnClickListener(v -> {
String fileName = "new-photo-name.jpg";
// Create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");
imageUri =
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 1231);
});
}
This is the onActivityResult method. Here I've used imageUri i.e. the global variable of type Uri which was initialized in the OnClickListener of the button in the setView() method above.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1231 && resultCode == Activity.RESULT_OK) {
try {
ContentResolver cr = getContentResolver();
try {
// Creating a Bitmap with the image Captured
Bitmap bitmap = MediaStore.Images.Media.getBitmap(cr, imageUri);
// Setting the bitmap as the image of the
imageViewCameraImage.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} catch (IllegalArgumentException e) {
if (e.getMessage() != null)
Log.e("Exception", e.getMessage());
else
Log.e("Exception", "Exception");
e.printStackTrace();
}
}
}
Remaining code...
// Asking user for storage permission
public void askPermission() {
// Checking if the permissions are not granted.
if (
ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(
this,
android.Manifest.permission.READ_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) {
// If not granted requesting Read and Write storage
ActivityCompat.requestPermissions(this, /*You can ask for multiple request by adding
more permissions to the string*/new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, 60);
} else {
// If permissions are granted we proceed by setting an OnClickListener for the button
// which helps the user pick the image
setView();
}
}
// This method is called after the permissions have been asked i.e. the dialog that says
// allow or deny
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Now by default we assume that the permission has not been granted
boolean allPermissionsGranted = false;
// Now we check if the permission was granted
if ( requestCode == 60 && grantResults.length > 0) {
// If all the permissions are granted allPermissionsGranted is set to true else false
allPermissionsGranted = grantResults[0] == PackageManager.PERMISSION_GRANTED
&&
grantResults[1] == PackageManager.PERMISSION_GRANTED;
}
// If permissions are granted we call the setView Method which prompts the user to pick
// an Image either by the clicking it now or picking from the gallery
if ( allPermissionsGranted ) {
setView();
}
}
}
These blocks of code are in sequence i.e. if you merge all these blocks you get the complete MainActivity.java class.
If you wan to Implement this app you need yo create a layout file with an ImageView with an id "imageViewCameraImage"and a button with an id "buttonCaptureImageFromCamera".
Hope this helps. I know this is long and I'm making it longer by writing this.
Regards,
Joel
simply use this Uri imageUri = data.getData();

onActivityResult not called when launching camera intent?

I have an activity where click of button camera activity is launched . Sometimes onActivityResult is called and sometimes not . Even after restarting the device the onActivityResult is not called nor does it restart the current activity . Any solution to this weird behavior ?
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
String imageUri = data.toURI();
Uri uri = Uri.parse(imageUri);
try {
mBitmap = Media.getBitmap(getContentResolver(), uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// if result is ok returns the bitmap
// mBitmap = (Bitmap) data.getExtras().get("data");
mImageView.setImageBitmap(mBitmap);
new Thread(postTheImage).start();
} else {
Toast.makeText(getApplicationContext(), "Error during capturing the image", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.capture_image_button) {
// Open the camera to capture the image
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
You are not providing the camera activity with the information it requires. You need to supply it a file URI.
Please refer to the following
onActivityResult returned from a camera, Intent null

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

why my file type is not supporting to send taken picture as MMS

I am implementing an MMS application for that i am using camera also.Main theme of main application was take picture using device camera ater that send that image as MMS to specified number.But while attahing the image i am getting error warning like
Unable to attach File not support
Please help to resolve my problem.
Thanks,
public class MMS extends Activity implements OnClickListener {
int TAKE_PHOTO_CODE = 0;
public static int count=0;
EditText preLoc,comeby;
Button ok,capture;
String photo;
String dir;
boolean GPS,flag;
String cityName=null;
String SubThorugh = null;
Intent i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mms);
preLoc = (EditText)findViewById(R.id.etPreLoc1);
comeby = (EditText)findViewById(R.id.etComing1);
ok = (Button)findViewById(R.id.bOK1);
capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(this);
ok.setOnClickListener(this);
i = getIntent();
GPS = i.getBooleanExtra("GPSneed", false);
ok.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.btnCapture:
capturePicture();
break;
case R.id.bOK1:
sendMMS();
preLoc.setText(cityName+SubThorugh);
break;
}
}
private void sendMMS() {
// TODO Auto-generated method stub
try {
Uri uri = Uri.parse(photo);
Intent i = new Intent(Intent.ACTION_SEND);
//i.putExtra("address",etnum.getText().toString());
//i.putExtra("sms_body",etmsg.getText().toString());
i.putExtra(Intent.EXTRA_STREAM,uri);
i.setType("image/*");
startActivity(i);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
#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");
Toast.makeText(getApplicationContext(), "photo saved as: "+photo, Toast.LENGTH_LONG).show();
}
}
private void capturePicture() {
//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();
// 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";
photo = file;
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);
}

Categories

Resources