I need to send an attachment along with some basic information by mail using my app. But the attachment is blank when I'm trying to invoke the mail activity using intent.
Here's the code for getting filepath and then sending the attachment
public class Pickafile extends AppCompatActivity {
TextView textFile;
static String FilePath;
private static final int PICKFILE_RESULT_CODE = 1;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
Button buttonPick = (Button) findViewById(R.id.buttonpick);
Button bp = (Button) findViewById(R.id.proceed);
textFile = (TextView) findViewById(R.id.textfile);
buttonPick.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_RESULT_CODE);
}
});
bp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
//intent.setType("text/plain");
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, " Report Particulars for " + MainActivity.name);
/*File root = Environment.getExternalStorageDirectory();
File file = new File(root, FilePath);
if (!file.exists() || !file.canRead()) {
return;
}*/
/*if(FilePath!=null)
{
intent.putExtra(Intent.EXTRA_STREAM, FilePath);
}*/
intent.putExtra(Intent.EXTRA_TEXT, MainActivity.testfunc());
intent.putExtra(Intent.EXTRA_STREAM, FilePath);
//startActivity(Intent.createChooser(intent, "Pick an Email provider"));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case PICKFILE_RESULT_CODE:
if (resultCode == RESULT_OK) {
FilePath = data.getData().getPath();
textFile.setText(FilePath);
}
break;
}
}
}
testfunc() is used to get body of the mail (name, age, phone,etc.)
Thanks in advance!
You should check the log if there is any error.
Possible case:
1. The file is too large, exceed maximum size of email attachment (20Mb for Gmail)
2. Missing READ_EXTERNAL_STORAGE permission if the file is saved in external storage.
3. Missing file_provider in manifest
Related
I integrate file picker in my application. so i set below code for that and i use this library for that.
library
public class FileChooserExampleActivity extends Activity {
private static final String TAG = "FileChooserExampleActivity";
private static final int REQUEST_CODE = 6384;
private static final int REQUEST_WRITE_STORAGE = 112;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a simple button to start the file chooser process
Button button = new Button(this);
button.setText(R.string.choose_file);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Display the file chooser dialog
showChooser();
}
});
setContentView(button);
}
private void showChooser() {
// Use the GET_CONTENT intent from the utility class
Intent target = FileUtils.createGetContentIntent();
// Create the chooser Intent
Intent intent = Intent.createChooser(
target, getString(R.string.chooser_title));
try {
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// The reason for the existence of aFileChooser
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE:
// If the file selection was successful
if (resultCode == RESULT_OK) {
if (data != null) {
// Get the URI of the selected file
final Uri uri = data.getData();
Log.i(TAG, "Uri = " + uri.toString());
try {
// Get the file path from the URI
final String path = FileUtils.getPath(this, uri);
Toast.makeText(FileChooserExampleActivity.this,
"File Selected: " + path, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("FileSelectorTestActivity", "File select error", e);
}
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
permissin:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
When i run above code in Android Marshmallow 6.0 device and i fetch image from sdcard than i get selected file path value as null so any idea how can i solve this ? your all suggestions are appreciable
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 using the following code for selecting file, it opens file
manager to select a file.
What I want is to show a dialog to choose the application first for
selecting a file, as shown in the image:
public class ActivityTestActivity extends Activity {
final int ACTIVITY_CHOOSE_FILE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) this.findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("file/*");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case ACTIVITY_CHOOSE_FILE: {
if (resultCode == RESULT_OK){
Uri uri = data.getData();
String filePath = uri.getPath();
}
}
}
}
}
The chooser options depends on the file type, so for example if you want to retrieve images you have to use image/* in setType method.
Edit: use */* for all types
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)
{
}