update contact picture on button from imageview android...? - android

in my application i fetch image from camera or gallery and save in imageview. now how can i save this imageview image as my contact picture image.
detail.java
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
//gallery picture
galary.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
});
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent image= new Intent(Intent.)
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
photo1.setImageBitmap(photo);
}
else
{
if (data != null && resultCode == RESULT_OK)
{
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 filePath = cursor.getString(columnIndex);
cursor.close();
if(photo != null && !photo.isRecycled())
{
photo = null;
}
photo = BitmapFactory.decodeFile(filePath);
photo1.setBackgroundResource(0);
photo1.setImageBitmap(photo);
}
else
{
Log.d("Status:", "Photopicker canceled");
}
}
}
}

setimage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Context context= getApplicationContext();
Bitmap icon= BitmapFactory.decodeResource(context.getResources(),
R.id.your_img_view);
try {
Intent myIntent = new Intent(); myIntent.setAction(Intent.ACTION_ATTACH_DATA);
myIntent.setType("image/jpeg");
myIntent.putExtra(Intent.EXTRA_STREAM, icon);
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
});
add:
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
in your manifest.

Related

how to select only camera capture images from gallery?

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);
}
}

Saving an image to custom folder with custom name

I am trying to build an app that captures an image ans saves the image to a custom folder with a custom name but i am unable to perform it.
Here is the code that I have written please go through the code and correct me where i am wrong.
public class MainActivity extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private Builder alertDialogBuilder;
final Context context = this;
String name_str;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
Button searchButton = (Button) this.findViewById(R.id.button2);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult1(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);
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);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
final Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
//Creating Alert Dialog Box
AlertDialog.Builder askname = new AlertDialog.Builder(MainActivity.this);
final EditText inputname = new EditText(this);
askname.setMessage("Enter Name");
askname.setView(inputname);
askname.setPositiveButton("OK",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
name_str = inputname.getEditableText().toString();
Toast.makeText(context,name_str,Toast.LENGTH_LONG).show();
dialog.dismiss();
/*if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
//photo = BitmapFactory.decodeByteArray(data,0,data.length);
imageView.setImageBitmap(photo);*/
String file ="/DCIM/VisitingCards/";
File folder = new File(Environment.getExternalStorageDirectory() + file);
System.out.println("File name is :"+folder);
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
//ON success
System.out.println("Directory Present");
System.out.println("In Success Case");
try {
FileOutputStream out = new FileOutputStream(folder);
photo.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
//ON failure
System.out.println("Directory not Present");
System.out.println("In Failure Case");
}
System.out.println("Before the Dialog builder");
}
});
askname.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
dialog.dismiss();
}
}); //End of alert.setNegativeButt
askname.show();
}
}
}
you should ask image name before intent and then used putExtra.
hope it may help you

Getting multiple images from Gallery

Right now I have an imageview. Clicking on the imageview I can get the option to select the gallery/camera intent.selecting on the required intent and the required picture I get the image in the imageview.This works fine for one single image.
How to get more than one picture.I mean the imageview[].Is there any code on this available?
Start Intent with EXTRA_ALLOW_MULTIPLE
Intent intent = new Intent( );
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent,
"select multiple images"), PICK_IMAGE_MULTIPLE);
On receiving side
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK){
if(requestCode == PICK_IMAGE_MULTIPLE){
String[] imagesPath = data.getStringExtra("data").split("\\|");
}
}
}
thanks for the response but here is my code.Now please let me know how to go ahead to have multiple images shown up in multiple imageviews.Also let me know if I need a placeholder like gridview for that?
ImageView image_view = new ImageView(this);
image_view.setId(field_id);
Uri selectedImage = Uri.parse(field_val);
String[] filePath = { MediaStore.Images.Media.DATA };
String picturePath;
Cursor c = getContentResolver().query(selectedImage,
filePath, null, null, null);
c.moveToFirst();
if(c.moveToFirst() && c.getCount() >= 1)
{
int columnIndex = c.getColumnIndex(filePath[0]);
picturePath = c.getString(columnIndex);
c.close();
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = 20;
Bitmap thumbnail = (BitmapFactory.decodeFile(
picturePath, bitmapOptions));
image_view.setImageBitmap(thumbnail);
image_view.setTag(selectedImage);
}
else {
image_view.setImageDrawable(getResources().getDrawable(
R.drawable.camera_launcher));
}
image_view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
getImage();
}
});
private void getImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(
this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
} else if (options[item].equals("Choose from Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bundle extras = data.getExtras();
try {
((ImageView) findViewById(R.id.imageid)).setImageBitmap((Bitmap) extras
.get("data"));
((ImageView) findViewById(R.id.imageid))).setTag(R.string.imgtag,
data.getDataString());
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
String picturePath;
Cursor c = getContentResolver().query(selectedImage, filePath,
null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
picturePath = c.getString(columnIndex);
c.close();
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = 20;
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath,
bitmapOptions));
((ImageView) findViewById(R.id.imageid))).setImageBitmap(thumbnail);
((ImageView) findViewById(R.id.imageid))).setTag(R.string.imgtag,
selectedImage.toString());
}
}
}

get selected image from gallery into imageview

I am facing a problem in selecting the image from a gallery and setting it into the imageview. Suppose I have two activities; mainActivity containing buttons for gallery and secondactivity containing the imageview in which the image has to be displayed.
b1.setOnClickListener(new OnClickListener() {
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);
}
});
Please give me the individual code for both....
here is the code to load an image from gallery:
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
#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));
}
}
}
with Picasso it can be done in single line and You dont need to make cursor query
I have extended it for better understanding :-
Pick Image
public void pickImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
Load Image
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();
Picasso.with(MainActivity1.this).load(selectedImageURI).noPlaceholder().centerCrop().fit()
.into((ImageView) findViewById(R.id.imageView1));
}
}
}
In my case work this solution
private void OpenGallery(){
Intent getImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
getImageIntent .setType("image/*");
startActivityForResult(getImageIntent , IMAGE_PICKER );
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode== IMAGE_PICKER && resultCode == RESULT_OK) {
Uri fullPhotoUri = data.getData();
imageView.setImageURI(fullPhotoUri);
}
}
You can share URI using putExtra("fullPhotoUri", fullPhotoUri.toString()) between activities.
Try this.
b1.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));
}
}
Here some examples
http://viralpatel.net/blogs/pick-image-from-galary-android-app/
http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/
To Pick Image from gallery, use this code.
btn_imageSetter.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"), IMAGE_PICKER);
}
});
Now use this Method to Set the image to the ImageViewer
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_PICKER && resultCode == RESULT_OK && null != data) {
try{
final Uri uriImage = data.getData();
final InputStream inputStream = getContentResolver().openInputStream(uriImage);
final Bitmap imageMap = BitmapFactory.decodeStream(inputStream);
iv_image.setImageBitmap(imageMap);
}
catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(context, "Image was not found", Toast.LENGTH_SHORT).show();
}
}
}

Android Picture Activity

I have created the activity for taking picture using Builtin image Capture activity but still not available to store picture in sdcard and view the captured image.The Intent is started and i am able to take the picture but when i click on ok(save), nothing haappens. Below is my activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picturelayout);
imageForUpload = (ImageView) findViewById(R.id.trackMePicture);
btnBack = (Button) findViewById(R.id.btnBack);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(this));
startActivityForResult(intent, TAKE_PHOTO_CODE);
}
/**
* #return
*/
private Uri getImageUri(Context context) {
// TODO Auto-generated method stub
File file =newFile(Environment.getExternalStorageDirectory(),context.getPackageName());
if(!file.exists())
file.mkdir();
File newFile=new File(file,new Date().toString()+".jpg");
Uri imagePath=Uri.fromFile(newFile);
return imagePath;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==TAKE_PHOTO_CODE ){
if(resultCode==-1){
Toast.makeText(getApplicationContext(), "Result code : "+resultCode, Toast.LENGTH_LONG).show();
//Uri imagePath=getImageUri();
Bitmap b;
try {
b = Media.getBitmap(getContentResolver(), getImageUri(this));
imageForUpload.setImageBitmap(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Toast.makeText(getApplicationContext(), "Result code : "+resultCode, Toast.LENGTH_LONG).show();
}
}
else{
Toast.makeText(getApplicationContext(), "Request code : "+requestCode, Toast.LENGTH_LONG).show();
}
}
There is no code in your onActivityResult() that would store the bitmap b in a file.
You can have a look at the PicHelper.storeBitmap() method from my Zwitscher app on how to do that.
Try doing this:
final Bundle extras = data.getExtras();
if (extras != null) {
Bitmap b = extras.getParcelable("data");
imageForUpload.setImageBitmap(b);
}
Here is how I do it (for taking picture or browsing the media directory), hope it can help:
static int SELECT_IMAGE = 2000;
static int CAMERA_REQUEST = 2001;
public void ProfilePictureDialog()
{
builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("").setPositiveButton("Browse images", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, SELECT_IMAGE);
}}).setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
}}).setNeutralButton("Take picture with camera", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}});
builder.show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode == RESULT_OK)
{
if (requestCode == CAMERA_REQUEST)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView profilePicture = (ImageView) findViewById(R.id.profilepic);
photo = Bitmap.createScaledBitmap(photo, profilePicture.getWidth(), profilePicture.getHeight(), true);
profilePicture.setImageBitmap(photo);
}
else if(requestCode == SELECT_IMAGE)
{
Uri selectedImagePath = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImagePath, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap photo = BitmapFactory.decodeFile(filePath);
ImageView profilePicture = (ImageView) findViewById(R.id.profilepic);
photo = Bitmap.createScaledBitmap(photo, profilePicture.getWidth(), profilePicture.getHeight(), true);
profilePicture.setImageBitmap(photo);
}
}
}

Categories

Resources