I want to attach imageView to send through Android mail services. This is how i get image from Gallery in Activity A:
public class Activity A extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_gallery);
private static int RESULT_LOAD_IMAGE = 1;
Button viewcards=(Button)findViewById(R.id.viewcards);
viewcards.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn =
{ MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Intent newdata = new Intent(SavedCards.this, Cardd.class);
newdata.putExtra("picture_path", picturePath);
startActivity(newdata);
}
}}
Transfer image to another class and and put imageView in Activity B:
public class Activity B extends Activity {
private ImageView cardimage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carddd);
String temp = getIntent().getStringExtra("picture_path");
cardimage = (ImageView) findViewById(R.id.cardimage);
cardimage.setImageBitmap(BitmapFactory.decodeFile(temp));
and in Activity B, i want to attach this image into e-mail by onClick.
txtsend=(TextView) findViewById(R.id.txtsend);
txtsend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
As a reference, how image can attach e-mail is here, but this is to pick from gallery.
How to do this by taken image which is already set on imageView. Thank you.
In your code you setup image to ImageView from file in Activity B. And you know path to this picture file. When you send email just attach this file.
Uri uri = Uri.parse("file://" + attachmentFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
You can use ImageView's getDrawable method see this http://developer.android.com/reference/android/widget/ImageView.html#getDrawable()
So something like,
Bitmap imgViewBmp = ((BitmapDrawable)cardImage.getDrawable()).getBitmap();
Should work.
PS: I would suggest you to first check the api reference docs for any class you want to use. Chances of finding what you are looking for is quite high. Next best thing would be to actually google what you want to solve, if that doesn't work then "rephrase" you words and try again. The chances that you will find your solution are doubled (if not more) with the latter!
Related
I have two activities in one i have a button and in second i have a ImageView, Now i want if i click on first activity's button then i go to gallery and pick an image when i select an image then it will automatically set in second activity's ImageView, My code is working when i set image in same acticity's imageView but i want set the image in second activity's ImageView. Please tell me what code i need to write in SecondActivity, My first Activity is(Select_Image):-
public class Select_Image extends Activity {
Button button;
public static final int PICK_FROM_GALLERY = 100;
Bitmap bitmap;
String imagePath = " ";
Uri uri;
ImageView image;
String path = " ";
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.select_image);
button = (Button)findViewById(R.id.select_btn);
image = (ImageView)findViewById(R.id.edit_image_id); //This is second Activity's ImageView id.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
galleryIntent();
}
});
}
public void galleryIntent(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_FROM_GALLERY);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == MainActivity.RESULT_OK) {
if (requestCode == PICK_FROM_GALLERY){
}
onSelectFromGalleryResult(data);
}
}
public void onSelectFromGalleryResult(Intent data){
if (data != null) {
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
}
catch (Exception e) {
e.printStackTrace();
}
}
uri = getImageUri(this, bitmap);
imagePath = getRealPathFromUri(uri);
image.setImageBitmap(bitmap);
}
private String getRealPathFromUri(Uri uri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = this.getContentResolver().query(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String ss = cursor.getString(column_index);
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
private Uri getImageUri(Select_Image select_image, Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Toast.makeText(select_image, "Image uploaded Successfully", Toast.LENGTH_LONG).show();
path = MediaStore.Images.Media.insertImage(select_image.getContentResolver(), bitmap, "Title", null);
return Uri.parse(path);
}
}
Second Activity(MainActivity)
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
In your onActivityResult, you got the result which is used by your current activity. If an image is selected you got your result on onActivityResult.Here, you can pass this imageUrl in your second activity and open image in your second activity. If you have still any doubt you can ask me.
To sum up, you want to interact with Activity A, start a task then receive the result in Activity B, it's impossible. But you can get the same behavior by 2 options:
Start Activity B on Activity A's button clicked. In Activity B's onCreate, start the image selector and get the result from there.
Start the image selector in A's button clicked, get the result and then pass the result to B using startActivity Intent.
Prefer the first option because it makes the code cleaner, Activity B does its own job and Activity A doesn't need to know about it. Moreover, you save some performance when serialize/deserialize the param from A to B, make the activity transition smoother
i have a registration activity in which user can upload image from his gallery, i want to pass this photo to navigation drawer in main activity & also to userprofile activity and i cant find the way how, code for RegistrationActivity:
code for RegistrationActivity:
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
de.hdodenhof.circleimageview.CircleImageView uplo;
private static int RESULT_LOAD_IMAGE = 1;
Context ctx = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final Button bRegister = (Button) findViewById(R.id.bRegister);
//upload profile image
uplo = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.uploadimage);
uplo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
bRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(RegisterActivity.this,MainActivity.class);
i.putExtra("picture", String.valueOf(uplo));
startActivity(i);
finish();
}
});
}
//to take the chosen image from user mobile gallery
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
uplo.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
a sample of code would be very helpful
thank you
Send the intent
Intent intent = new Intent(Home.this, Upload.class);
intent.putExtra("picture", thumbnail);
and receive in other other activity
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("picture");
hope it work :)
If you want to send the image, just send the path.
change it to.
bRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(RegisterActivity.this,MainActivity.class);
i.putExtra("picture", picturePath);// send path
startActivity(i);
finish();
}
});
and receive the path in another activity.
and set as..
.setImageBitmap(BitmapFactory.decodeFile(picturePath));
2nd If you want to access picture on other activities
Just put the path in shared preferences as USER_DP string. and get it whenever you want. eg.
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
shared.edit().putString("USER_DP",picturePath).commit();// like this
You can use below code to save your path in to shared preferences and you can fetch it as per your requirement. initialise shared preference
SharedPreferences sharedPref = context.getSharedPreferences(SHARED,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
save path
public void savePath(String key,String path){
editor.putString(key, path);
editor.commit();
}
retrieve path in your specific class--
` public String getPath(String key ){
if (sharedPref.contains(key)){
String path="";
path=sharedPref.getString(key, " ");
return path;
}
return "";
} `
i searched and i found how to can to get image from gallery and show it in image view.this is a my source
public class CredoUploadFile extends Activity {
private EditText credouploadfileuserid, credouploadfileusername,
credouploadfileusersurname;
private int RESULT_LOAD_IMAGE = 1;
private int choosebutton;
private RoundedImageView credouploadcenter1left,credouploadcenter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_credo_upload_file);
credouploadcenter1 = (RoundedImageView) findViewById(R.id.credouploadcenter1);
credouploadcenter1left = (RoundedImageView) findViewById(R.id.credouploadcenter1left);
credouploadcenter1left.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
choosebutton=1;
}
});
credouploadcenter1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
choosebutton=2;
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
if(choosebutton==1)
credouploadcenter1left.setImageBitmap(BitmapFactory
.decodeFile(picturePath));
if(choosebutton==2)
credouploadcenter1.setImageBitmap(BitmapFactory
.decodeFile(picturePath));
}
}
}
i can to show image from gallery in image view,but i have two imageviews and when i click second imageview and choose some image result is first image view.i can't to show image only imageview witch i clicked.
how i can solve my problem? if anyone knows solution please help me
In onActivityResult you use the same variable credouploadcenter1left for both if...
Try:
if(choosebutton==1)
credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
if(choosebutton==2)
credouploadcenter1.setImageBitmap(BitmapFactory .decodeFile(picturePath));
if(choosebutton==1)
credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
if(choosebutton==2)
credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Here is the error, independently of your choose, you will set image always on credouploadcenter1left.
You have to set it on centeruploadcenter1 when you choose second button.
How Can I put image from directory instead of load from media. I load from gallery like this
String[] filePathColumn = { MediaStore.Images.Media.DATA };. How can load from /assets or /drawable-mdpi ? This is my code. Kindly tell me how can i used this code to add image from my directory?
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
You can put the respective drawables in the android drawables folders and Android would automatically choose the required drawable based upon the device display density. You can set the drawables as
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.image));
You can also take help from here.
im trying to access and select an image from my phone galley onto my application imageView, using android. So far ive managed to set get to the galley and select the image i want to use, the only problem is that once ive selected the image i want i cant get it onto the imageview. the problem seems to be that ive got the imageview class seperate from my main class. ive checked and cant find a solution.
heres the code for the mediagalley class
public class MediaGallery extends Activity {
public static final int SELECT_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == SELECT_IMAGE) {
Uri selectedImage = data.getData();
String path = getPath(selectedImage);
Bitmap bitmapImage = BitmapFactory.decodeFile(path);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bitmapImage);
}
}
public String getPath(Uri uri) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, filePathColumn, null,null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
return cursor.getString(columnIndex);
}
}
and the main class code
case R.id.gallery_button:
MediaGallery activ1 = new MediaGallery();
Intent gallery = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, 1);
activ1.onActivityResult(1, 1, gallery);
break;
any help u gave give il be very greatful
You have declared MediaGallery is another Activity. Are you trying to show an ImageView on the same activity from which you select the button to launch the picker, or in a new activity? If the former, put that onActivityResult code into your main class. If the latter, then your main class should pass the Intent data to the MediaGallery class.