我对这个应用程序感到害怕。我正在开发一个可以从图像视图更改联系人照片的应用程序。它不会产生任何错误,但甚至不会改变联系人图像。我有一个疑问,如果它是OnActivityResult()方法。请纠正我。这是OnActivityResult()方法的代码。
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case IMAGE_PICK:
this.imageFromGallery(resultCode, data);
break;
case IMAGE_CAPTURE:
this.imageFromCamera(resultCode, data);
break;
case PICK_PHOTO:
if(resultCode == RESULT_OK){
// Getting the uri of the picked photo
Uri updateImageView = data.getData();
InputStream imageStream = null;
try {
// Getting InputStream of the selected image
imageStream = getContentResolver().openInputStream(updateImageView);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Creating bitmap of the selected image from its inputstream
image = BitmapFactory.decodeStream(imageStream);
// Getting reference to ImageView
// ImageButton ibPhoto = (ImageButton) findViewById(R.id.photo);
// Setting Bitmap to ImageButton
photo.setImageBitmap(image);
}
break;
default:
break;
}
}
}
这里也是我的onClick()代码。
save.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
int position=getIntent().getExtras().getInt("bmp");
int po=ops.size();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
if(image!=null)
{ // If an image is selected successfully
image.compress(Bitmap.CompressFormat.PNG , 100, stream);
// ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG , 75, stream);
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, position) // here position is _ID where I'm inserting image
.withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO,stream.toByteArray())
.build());
try {
stream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
Log.d("Image Null", "True");
}
try{
// Executing all the insert operations as a single database transaction
getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
Toast.makeText(getBaseContext(), "Contact Image change successfully", Toast.LENGTH_SHORT).show();
}
catch (RemoteException e) {
e.printStackTrace();
}catch (OperationApplicationException e) {
e.printStackTrace();
}
} });