I am trying to create an application where you can take a picture and then email it to someone. At the moment I can take a picture and set my background as this picture:
public class Camera extends Activity implements View.OnClickListener{
ImageButton ib;
Button b;
ImageView iv;
Intent i;
final static int cameraData = 0;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
initialize();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmp = BitmapFactory.decodeStream(is);
}
private void initialize(){
ib = (ImageButton) findViewById(R.id.ibTakePic);
b = (Button) findViewById(R.id.bSetWall);
iv = (ImageView) findViewById(R.id.ivReturnedPic);
b.setOnClickListener(this);
ib.setOnClickListener(this);
}
#Override
public void onClick(View v) {
File mImageFile;
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.bSetWall:
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
bmp = (Bitmap)extras.get("data");
iv.setImageBitmap(bmp);
}
}
}
I have a separate application where I can take in user input and email it to a predefined address:
public void onClick(View v) {
// TODO Auto-generated method stub
convertEditTextVarsIntoStringsAndYesThisIsAMethodWeCreated();
String emailaddress[] = { "info#sklep.com", "", };
String message = emailAdd + name + beginning;
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
startActivity(emailIntent);
}
How do I go about emailing the picture that I have taken? Where is it saved and how do I access it so that I can email it?
Many Thanks
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File output = new File(dir, "camerascript.png");
cPath = output.getAbsolutePath();
Bitmap b = BitmapFactory.decodeFile(cPath);
Bitmap out = Bitmap.createScaledBitmap(b, 320, 480, false);
FileOutputStream fout;
try{
fout = new FileOutputStream(output);
out.compress(Bitmap.CompressFormat.PNG, 100, fout);
fout.flush();
fout.close();
b.recycle();
out.recycle();
}catch(Exception e){
e.printStackTrace();
}
}
}
after that in your sending mail method
public void sendMail(){
Log.e("sendMail", "v r in sendMail");
i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text");
i.putExtra(Intent.EXTRA_EMAIL, new String[] {"abc#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT,"subject...");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(cPath));
i.putExtra(Intent.EXTRA_TEXT, "Body of Email....");
startActivityForResult(Intent.createChooser(i, "send mail...."),EMAIL_SUCCESS);
}
above code will help u..
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Uri uri=data.getData();
sendmail(uri);
}
and
private void sendmail(Uri uri)
{
i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text");
i.putExtra(Intent.EXTRA_EMAIL, new String[] {"abc#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT,"subject...");
i.putExtra(Intent.EXTRA_STREAM, uri);
i.putExtra(Intent.EXTRA_TEXT, "Body of Email....");
startActivityForResult(Intent.createChooser(i, "send mail...."),EMAIL_SUCCESS);
}
It may works.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
im doing online application. One of the functions are user can choose photo from their gallery or take photo from their camera.Now i have trouble in onActivityResult method where i failed to merge it.Hope anyone can help me to merge it so that the images that user choose or took can view in my ImageView (image_view). This is my main activity:
**private static final int REQUEST_CODE = 1;
private Button button_1;
public int TAKE_PICTURE = 1;
private ImageView image_view;
private Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sell);
image_view = (ImageView) findViewById(R.id.resul);
button_1 = (Button) findViewById(R.id.button4);
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
}
});
Button buttonLoadImage = (Button) findViewById(R.id.button3);
buttonLoadImage.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
try {
// We need to recyle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
image_view.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
super.onActivityResult(requestCode, resultCode, data);
}
#Override
protected void onActivityResult2(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri targetUri = data.getData();
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
image_view.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
enter code here
You do not need create two onActivityResult() method for two startActivityForResult call. Just provide different request code in both startActivityForResult call. and onActivityResult will notify you with given request code to identify the result.
private static final int TAKE_PICTURE = 100;
private static final int CHOOSE_PICTURE = 101;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sell);
image_view = (ImageView) findViewById(R.id.resul);
button_1 = (Button) findViewById(R.id.button4);
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// Here provide TAKE_PICTURE request code to identify the IMAGE_CAPTURE result in onActivityResult method.
startActivityForResult(intent, TAKE_PICTURE);
}
});
Button buttonLoadImage = (Button) findViewById(R.id.button3);
buttonLoadImage.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
// Here provide CHOOSE_PICTURE request code to identify the ACTION_PICK result in onActivityResult method. android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, CHOOSE_PICTURE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK)
// Do something with take picker result
}else if (requestCode == CHOOSE_PICTURE && resultCode == Activity.RESULT_OK)
// Do something with choose image result
}
}
The app has 2 buttons on the screen, 1 for taking a photo and attaching it on the screen and the second to attach the photo on gmail & send it to someone. I'm using this code for the second button
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "test#gmail.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "test");
intent.putExtra(Intent.EXTRA_TEXT, "text");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
}
But it doesn't attach the photo on gmail. It might be wrong. Here's the rest of the code.
public class MainActivity extends Activity {
private static int TAKE_PICTURE = 1;
private Uri imageUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendbutton = (Button) findViewById(R.id.sendbutton);
sendbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "test#gmail.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "test");
intent.putExtra(Intent.EXTRA_TEXT, "text");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent,
"Choose an Email client :"));
}
});
Button cameraButton = (Button) findViewById(R.id.button_camera);
cameraButton.setOnClickListener(cameraListener);
}
private OnClickListener cameraListener = new OnClickListener() {
public void onClick(View v) {
takePhoto(v);
}
};
public void takePhoto(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView) findViewById(R.id.image_view_camera);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap);
Toast.makeText(MainActivity.this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "failed to load",
Toast.LENGTH_LONG).show();
}
}
}
}
}
Try this
You can see more at this link: Sharing content in android using action send intent
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "test");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "text");
sharingIntent.putExtra(Intent.EXTRA_STREAM, targetUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
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);
}
I am calling the camera and capturing images, I will have to capture 10 images one by one and store them on SD card before I can set them to the Image view. Please check my below code, it does not set to the image view.
How would I store it on the SD card and retrieve it to set to the image view? How would I name the images before storing?
In the first activity I am calling the camera:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
init();
}
private void init() {
String extStorageDirectory = Environment.getExternalStorageDirectory()
+ "/testing";
File xmlDirectory = new File(extStorageDirectory);
if (!xmlDirectory.exists())
xmlDirectory.mkdirs();
iv1 = (ImageView) findViewById(R.id.iv1);
}
private OnClickListener onBtnClicked = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case PHOTO:
Intent selectImageIntent = new Intent(first.this,
second.class);
startActivityForResult(selectImageIntent, 1);
break;
}
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("result");
Log.d("*****************",
"inside onactivityresult in main activity=" + result);
Bitmap bitmap = BitmapFactory.decodeFile(result);
iv1.setImageBitmap(bitmap);
iv1.setScaleType(ScaleType.FIT_XY);
}
}
}
And in my second activity I am capturing the image and passing it to the first activity:
private void init() {
picturePath = Environment.getExternalStorageDirectory() + "/Camera/"
+ "test.jpg";
System.out.println("thumbnail path~~~~~~" + picturePath);
File file = new File(picturePath);
outputFileUri = Uri.fromFile(file);
}
public void startCamera() {
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, IMAGE_CAPTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
Intent returnIntent = new Intent();
returnIntent.putExtra("result", picturePath);
setResult(RESULT_OK, returnIntent);
finish();
}
}
}
it is not a good Practice to initialize your object inside Other Method.
remove this line iv1 = (ImageView) findViewById(R.id.iv1); from init() method and Change your OnCreate() to Below Way.
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Initialize here
iv1 = (ImageView) findViewById(R.id.iv1);
mContext = this;
init();
}
hope it will help you.
use this code & put your file name & path, camera will capture the image & store it with given name
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (!APP_FILE_PATH_Media.exists())
{
APP_FILE_PATH_Media.mkdirs();
}
uriSavedImage =new File(APP_FILE_PATH_Media+ "/" +
"filename"+ ".jpeg");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(uriSavedImage));
startActivityForResult(cameraIntent, CAMERA_REQUEST);
in onActivityResult() use this code to set imageView
try
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
bm = BitmapFactory.decodeFile(uriSavedImage.getAbsolutePath(), options);
}
catch(Exception ee)
{
}
in my camera intent:
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
this part gives me a null pointer exception.
can anyone explain why and what need to be changed??
button_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, TAKE_PICTURE);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
Try the following,
public class Camera extends Activity
{
private static final int CAMERA_REQUEST = 1888;
private String selectedImagePath;
WebView webview;
String fileName = "capturedImage.jpg";
private static Uri mCapturedImageURI;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
Random randomGenerator = new Random();randomGenerator.nextInt();
String newimagename=randomGenerator.toString()+".jpg";
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + newimagename);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//write the bytes in file
try {
fo = new FileOutputStream(f.getAbsoluteFile());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
uri=f.getAbsolutePath();
//this is the url that where you are saved the image
}
}