I am trying to send an e-mail with a file attached,So I have downloaded sample project and working on it.The problem is everytime I get the same message "couldnt attach file (sorry for my english)
So far I have this .
public class MainActivity extends Activity implements OnClickListener {
EditText et_address, et_subject, et_message;
String address, subject, message, file_path;
Button bt_send, bt_attach;
TextView tv_attach;
private static final int PICK_IMAGE = 100;
Uri URI = null;
int columnindex;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
bt_send.setOnClickListener(this);
bt_attach.setOnClickListener(this);
}
private void initializeViews() {
et_address = (EditText) findViewById(R.id.et_address_id);
et_subject = (EditText) findViewById(R.id.et_subject_id);
et_message = (EditText) findViewById(R.id.et_message_id);
bt_send = (Button) findViewById(R.id.bt_send_id);
bt_attach = (Button) findViewById(R.id.bt_attach_id);
tv_attach = (TextView) findViewById(R.id.tv_attach_id);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_attach_id:
openGallery();
break;
case R.id.bt_send_id:
try {
address = et_address.getText().toString();
subject = et_subject.getText().toString();
message = et_message.getText().toString();
String emailAddresses[] = { address };
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
emailAddresses);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
if (URI != null)
emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
startActivity(emailIntent);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
private void openGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
PICK_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
columnindex = cursor.getColumnIndex(filePathColumn[0]);
file_path = cursor.getString(columnindex);
// Log.e("Attachment Path:", attachmentFile);
tv_attach.setText(file_path);
URI = Uri.parse("file://" + file_path);
cursor.close();
}
}
Your processing of the Uri returned from the gallery looks overcomplicated. You are taking a Uri and processing it to produce a Uri.
I think your onActivityResult() method should be more like:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
URI = data.getData();
}
}
This is better as it's also not good practice to pass a "file://" Uri to an external application in case the external email application doesn't have access to the file.
Related
I want to pick only those images from gallery which are clicked from camera,not present from other sources.
Cursor imagecursor = getApplicationContext().getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID}, null,
null, null);
This query gives me all the images.
I want the path to camera images irrespectice of gallery and camera intent.Is there some generic method which returns the path of captured images path
here is an example hope it help!!
package com.mahesh.gallerytestapp;
public class MainActivity extends Activity {
Uri selectedImageUri;
String selectedPath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.bGallery);
Button bCam= (Button) findViewById(R.id.bCamera);
ImageView preview = findViewById(R.id.preview);
bCam.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 100);
}
});
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openGallery(10);
}
});
}
public void openGallery(int req_code){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select file to upload "), req_code);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(data.getData() != null){
selectedImageUri = data.getData();
}else{
Log.d("selectedPath1 : ","Came here its null !");
Toast.makeText(getApplicationContext(), "failed to get Image!", 500).show();
}
if (requestCode == 100 && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
selectedPath = getPath(selectedImageUri);
preview.setImageURI(selectedImageUri);
Log.d("selectedPath1 : " ,selectedPath);
}
if (requestCode == 10)
{
selectedPath = getPath(selectedImageUri);
preview.setImageURI(selectedImageUri);
Log.d("selectedPath1 : " ,selectedPath);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
I want to open gallery with multiple image selection functionality and i am using following code.
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);
It opens gallery app but doesn't let me choose multiple images.
This worked for me from api22 to api29.
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 105);
then in activity result overmethod add this code.
if (resultCode == RESULT_OK && requestCode == 105) {
ClipData clipData = data.getClipData();
if (clipData != null) {
for (int i = 0; i < clipData.getItemCount(); i++) {
Uri imageUri = clipData.getItemAt(i).getUri();
// your code for multiple image selection
}
} else {
Uri uri = data.getData();
// your codefor single image selection
}
Note: after you got the gallery screen hold the image little longer. then in top right click "open". it will allow you to select multiple images.
To select multiple images I use this :
Declare a variable
ActivityResultLauncher activityResultLauncher;
2.Call Register Method in OnCreate Method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload_ad_images);
RegisterOpenImageDialog();
}
3.this is the register method
private void RegisterOpenImageDialog() {
activityResultLauncher = registerForActivityResult(new ActivityResultContracts.GetMultipleContents(), new ActivityResultCallback<List<Uri>>() {
#Override
public void onActivityResult(List<Uri> result) {
if (result != null) {
int x = result.size();
//Do What you Want Here ................
}
}
});
}
4.when the user click select image button
private void btnSelectImage() {
activityResultLauncher.launch("image/*");
}
Click and hold to select multiple image. Don't just click on image. Also you can receive those image using this.
int PICK_IMAGE_MULTIPLE = 1;
String imageEncoded;
List<String> imagesEncodedList;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
// When an Image is picked
if (requestCode == 1 && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
String[] filePathColumn = { MediaStore.Images.Media.DATA };
imagesEncodedList = new ArrayList<String>();
if(data.getData()!=null){
Uri mImageUri=data.getData();
// Get the cursor
Cursor cursor = getContentResolver().query(mImageUri,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageEncoded = cursor.getString(columnIndex);
cursor.close();
}else {
if (data.getClipData() != null) {
ClipData mClipData = data.getClipData();
ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
for (int i = 0; i < mClipData.getItemCount(); i++) {
ClipData.Item item = mClipData.getItemAt(i);
Uri uri = item.getUri();
mArrayUri.add(uri);
// Get the cursor
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageEncoded = cursor.getString(columnIndex);
imagesEncodedList.add(imageEncoded);
cursor.close();
}
Log.v("LOG_TAG", "Selected Images" + mArrayUri.size());
}
}
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
super.onActivityResult(requestCode, resultCode, data);
}
try this:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int SELECT_PICTURE = 100;
private static final String TAG = "MainActivity";
CoordinatorLayout coordinatorLayout;
FloatingActionButton btnSelectImage;
AppCompatImageView imgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Find the views...
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
btnSelectImage = (FloatingActionButton) findViewById(R.id.btnSelectImage);
imgView = (AppCompatImageView) findViewById(R.id.imgView);
btnSelectImage.setOnClickListener(this);
}
/* Choose an image from Gallery */
void openImageChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
// Get the url from data
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
// Get the path from the Uri
String path = getPathFromURI(selectedImageUri);
Log.i(TAG, "Image Path : " + path);
// Set the image in ImageView
imgView.setImageURI(selectedImageUri);
}
}
}
}
public String getPathFromURI(Uri contentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
#Override
public void onClick(View v) {
openImageChooser();
}
}
Add Permission in manifest file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I am working on app for editing photos.
I have a button in first activity and ImageView in second activity. When I click the button it would open gallery and I would be able to select an image. The selected image needs to appear in my ImageView in second activity but it doesn't. For time being i am displaying image in first activity it self but can anyone suggest me how to display that image in next activity.
Below is my code.
public class Camera_Gallery_Option extends Activity {
private static final int CAMERA_REQUEST = 1888;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_gallery_option);
Button galleryButton= (Button) findViewById(R.id.button1);
galleryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
imageview.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Pass the selectedImageUri to the second activity and set the uri to image view in the second activity ?
startActivity(new Intent(this, SecondActivity.class).setData(selectedImageUri));
In the SecondActivity:
protected void onCreate(Bundle savedInstanceState) {
...
imageView.setImageURI(getIntent().getData());
}
Whenever I try to click on the browse button , I want to browse my gallery and select a photo from it and display it in a new activity.
Until I displayed the photo on the same activity there was no error , but as soon I change the code to transfer the photo on another intent there is a force close error
Here is my code:
Main screen :
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent1 = new Intent(MainActivity.this,Edit.class);
startActivity(intent1);
}
});
Another screen :
public class Edit extends Activity
{
private String selectedImagePath;
private static final int SELECT_PICTURE = 1;
private ImageView img;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
img = (ImageView)findViewById(R.id.ImageView1);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
My application adds a photo from your gallery to an ImageView just fine, but if I try and change the image (after one has already been selected) my app force closes. Is there a way to achieve this?
here is my code..
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.myimageview);
((Button) findViewById(R.id.taptoadd))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
Set the image through code .
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);