我想从联系人那里挑选照片。我从堆栈溢出中获得了以下代码,但是我需要传递给该方法的参数是什么?请帮帮我
public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
if (input == null) {
return null;
}
return BitmapFactory.decodeStream(input);
}
答案 0 :(得分:3)
我不知道您是否使用相同的链接,但您可以参考this。
我还没试过这个。但是参考这个链接,我想,你必须传递ContactsContract.Contact表的_id。
Cursor cursor=getContentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
cursor.moveToFirst();
long id=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
loadContactPhoto(getContentResolver(),id)
应作为参数传递到此处。
答案 1 :(得分:0)
我希望这可以帮助您获取联系人图片,电话号码和电子邮件地址。
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;
import com.example.bean.MemberBean;
import com.example.db.AppDBManager;
public class AddContactActivity extends Activity implements OnClickListener {
Button _chooseContactButton, _submitButton;
EditText name, number, email, relation;
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
ImageView _contactPic;
String imagePath = "",Name="",Number="",Relation="",emailid="";
private Uri fileUri;
private Uri uriContact;
private String contactID;
public static String localNumber=null;
private PopupWindow myPopupWindow=null;
File photo;
String _imageUrl="";
String _imageUrl1;
String _time="";
private static final int SELECT_PICTURE = 1;
private static final int SELECT_CONTACT = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main1);
_chooseContactButton = (Button) findViewById(R.id.button1);
_submitButton = (Button) findViewById(R.id.button_submit);
name = (EditText) findViewById(R.id.editText1);
number = (EditText) findViewById(R.id.editText2);
email = (EditText) findViewById(R.id.editText3);
relation = (EditText) findViewById(R.id.editText4);
_contactPic = (ImageView) findViewById(R.id.imageView1);
_contactPic.setOnClickListener(this);
_chooseContactButton.setOnClickListener(this);
_submitButton.setOnClickListener(this);
Calendar cal = Calendar.getInstance();
int millisecond = cal.get(Calendar.MILLISECOND);
int second = cal.get(Calendar.SECOND);
int minute = cal.get(Calendar.MINUTE);
int hourofday = cal.get(Calendar.HOUR_OF_DAY);
_time="image_"+hourofday+""+minute+""+second+""+millisecond;
Intent intent=getIntent();
Name =intent.getStringExtra("name");
Number =intent.getStringExtra("mobile");
localNumber=intent.getStringExtra("mobile");
Relation =intent.getStringExtra("relation");
emailid =intent.getStringExtra("email");
_imageUrl1 =intent.getStringExtra("image");
name.setText(Name);
number.setText(Number);
relation.setText(Relation);
email.setText(emailid);
if(_imageUrl1!=null)
{
if(_imageUrl1.length()==0) {
_contactPic .setBackgroundResource(R.drawable.back_b);
}
else{
Drawable d=BitmapDrawable.createFromPath(_imageUrl1);
_imageUrl=_imageUrl1;
_contactPic.setBackgroundDrawable(d);
}
}
}
@Override
public void onClick(View V) {
if (V.equals(_chooseContactButton)) {
Intent nyintent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(nyintent, SELECT_CONTACT);
} else if (V.equals(_submitButton)) {
if (isInputValid()) {
String Name = name.getText().toString();
String Number = number.getText().toString();
String Relation = relation.getText().toString();
String emailid = email.getText().toString();
AppDBManager dbManager = new AppDBManager(this);
dbManager.openDatabase();
MemberBean member = new MemberBean();
member.setName(Name);
member.setMobile(Number);
member.setRelation(Relation);
member.setEmail(emailid);
member.setImage(_imageUrl);
dbManager.insertMember(member);
dbManager.closeDatabase();
Intent intent = new Intent(this, ListShow.class);
startActivity(intent);
finish();
}
} else if (V.equals(_contactPic)) {
final View myPopupView; myPopupView = getLayoutInflater().inflate(
R.layout.pickgallerychooser, null);
myPopupWindow = new PopupWindow(myPopupView,
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
myPopupWindow.showAtLocation(myPopupView, Gravity.CENTER, 0, 0);
ImageButton gallery = (ImageButton) myPopupView
.findViewById(R.id.gallery_button);
ImageButton camera = (ImageButton) myPopupView
.findViewById(R.id.camera_button);
gallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myPopupWindow.dismiss();
Intent nyintent = new Intent();
nyintent.setType("image/*");
nyintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(nyintent, "Select Picture"),
SELECT_PICTURE);
}
});
camera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myPopupWindow.dismiss();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent,
CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
});
}
}
@SuppressWarnings("deprecation")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
_contactPic.setBackgroundResource(R.drawable.imageview_background);
try {
if (requestCode == SELECT_CONTACT && resultCode == RESULT_OK
&& null != data) {
uriContact = data.getData();
email.setText("");
number.setText("");
name.setText("");
retrieveContactNumber();
retrieveContactPhoto();
retrieveContactName();
} else if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK
&& null != data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == SELECT_PICTURE && resultCode == RESULT_OK && null != data)) {
//select gallery
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();
Drawable drawable ;
_imageUrl = filePath;
drawable = BitmapDrawable.createFromPath(filePath);
_contactPic.setBackgroundDrawable(drawable);
}
} else if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE
&& resultCode == RESULT_OK && null != data) {
//camera picture
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Drawable drawable = new BitmapDrawable(thumbnail);
_contactPic.setBackgroundDrawable(drawable);
saveBitmap(thumbnail);
}
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
private void retrieveContactPhoto() {
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts
.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI,
new Long(contactID)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
Drawable drawable = new BitmapDrawable(photo);
_contactPic.setBackgroundDrawable(drawable);
Uri tempUri = getImageUri(getApplicationContext(), photo);
File finalFile = new File(getRealPathFromURI(tempUri));
_imageUrl=finalFile.getAbsoluteFile().toString();
Log.e("hiiiiiiiiiiiiiiiiii","url is"+_imageUrl);
}
assert inputStream != null;
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void retrieveContactNumber() {
String contactNumber="";
Cursor cursorID = getContentResolver().query(uriContact,
new String[] { ContactsContract.Contacts._ID }, null, null,
null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID
.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Cursor cursorPhone = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND "
+ ContactsContract.CommonDataKinds.Phone.TYPE + " = "
+ ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[] { contactID }, null);
int ColumeIndex_HAS_PHONE_NUMBER = cursorPhone.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
Log.e("sanjay ", "hiiiiiiiiiiiiiiiiiiiiiiiii"+ColumeIndex_HAS_PHONE_NUMBER);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone
.getString(cursorPhone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
number.setText(contactNumber);
}
private void retrieveContactName() {
String contactName = null;
String email1 = null;
Cursor cursor = getContentResolver().query(uriContact, null, null,null, null);
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
name.setText(""+ contactName);
Cursor emailcur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{contactID}, null);
while (emailcur.moveToNext()) {
email1 = emailcur.getString(emailcur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emailcur.close();
email.setText(email1);
}
private Boolean isInputValid() {
Boolean isValid = true;
Name = name.getText().toString();
Number = number.getText().toString();
Relation = relation.getText().toString();
emailid = email.getText().toString();
if (Name.length() == 0) {
isValid = false;
name.requestFocus();
Toast.makeText(this, "Enter Name", Toast.LENGTH_LONG).show();
} else if (Number.length() == 0) {
isValid = false;
number.requestFocus();
Toast.makeText(this, "Enter Contact Person Number", Toast.LENGTH_LONG).show();
} else if (Relation.length() == 0) {
isValid = false;
relation.requestFocus();
Toast.makeText(this, "Enter Relation of Person", Toast.LENGTH_LONG).show();
} else if (emailid.length() == 0) {
isValid = false;
email.requestFocus();
Toast.makeText(this, "Enter Valid Email Id", Toast.LENGTH_LONG).show();
}
else if ((!emailid.contains("@") || !emailid.contains(".")) || !(emailid.lastIndexOf("@")<emailid.lastIndexOf(".")) || !(emailid.lastIndexOf("@")==emailid.indexOf("@"))|| emailid.contains(" ")) {
isValid = false;
email.requestFocus();
Toast.makeText(this, "Enter Valid Email Id", Toast.LENGTH_LONG).show();
}
return isValid;
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = android.provider.MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, null, null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
public void saveBitmap(Bitmap bmp)
{
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +"/DCIM";
try
{
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, _time);
_imageUrl=file.getAbsolutePath();
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 80, fOut);
fOut.flush();
fOut.close();
}
catch (Exception e) {
Log.e("errr in ","inserting the image at parictular location");
}
}
@Override
public void onBackPressed() {
Intent intent=getIntent();
if(intent.getStringExtra("value")!=null)
{
super.onBackPressed();
}
else if(myPopupWindow!=null && myPopupWindow.isShowing())
{
myPopupWindow.dismiss();
}
else
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(AddContactActivity.this);
alertDialogBuilder.setTitle("Confirm ?");
alertDialogBuilder
.setMessage("It will close the application.\nDo you want to continue")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
finish();
}})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
}