I wanted some help with inserting a photo into contacts,as far as i have researched i found two ways we can insert contacts in our phone, one is starting the contacts activity of the phone, and the other is by inserting the values directly in the phone, i am using the first method, where we have to start the intent, when we start the intent i am not getting any solution to add image, i have options to add other minor details like name, work place, etc. The problem with second method is that, it doesn't let us know if the contact is already added, and this might cause an error, it might create duplicate contacts. What do you suggest i can do ?
What i was doing till now is
Intent contactIntent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,ContactsContract.Contacts.CONTENT_URI);
contactIntent.setData(Uri.parse("tel:" +"+91"+mMobile));
contactIntent.putExtra(ContactsContract.Intents.Insert.NAME, name);
contactIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
contactIntent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, mobileEx);
startActivity(contactIntent);
For passing profile image via intent for contacts Editor screen, you can do something like shown below
Intent contactIntent = new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,ContactsContract.Contacts.CONTENT_URI);
contactIntent.setData(Uri.parse("tel:" +"+91"+mMobile));
contactIntent.putExtra(ContactsContract.Intents.Insert.NAME, name);
contactIntent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
contactIntent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, mobileEx);
Bitmap bit = BitmapFactory.decodeResource(getResources(), R.drawable.profile_image);
ArrayList<ContentValues> data = new ArrayList<ContentValues>();
ContentValues row = new ContentValues();
row.put(Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
row.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bitmapToByteArray(bit));
data.add(row);
contactIntent.putParcelableArrayListExtra(Insert.DATA, data);
startActivity(contactIntent);
And logic for converting bitmap to byteArray is
private byte[] bitmapToByteArray(Bitmap bit) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
Related
I'm trying to make a simple Android app for my own phone that looks at my Gallery, and for each image/video file:
creates a Bitmap thumbnail of the file
records the file's absolute path on the phone
Essentially, I'll have the following data model:
public class MediaFileModel {
private Bitmap thumbnail;
private String absPath;
// ctor, getters and setters omitted for brevity
}
And I need something that looks at all the files in my Gallery and yields a List<MediaFileModel>. My best attempt is here:
public List<MediaFileModel> getAllGalleryMedia() {
String[] projection = { MediaStore.MediaColumns.DATA };
List<MediaFileModel> galleryMedia = new ArrayList<>();
Cursor cursor = getActivity().getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null);
// I believe here I'm iterating over all the gallery files (???)
while(cursor.moveToNext()) {
MediaFileModel next = new MediaFileModel();
// is this how I get the abs path of the current file?!?
String absPath = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
Bitmap thumbnail = null;
if (true /* ??? cursor is pointing to a media file that is an image/photo ??? */) {
thumbnail = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(absPath), 64, 64);
} else {
// else we have a video (I assume???)
thumbnail = ThumbnailUtils.createVideoThumbnail(absPath, MediaStore.Images.Thumbnails.MINI_KIND);
}
next.setThumbnail(thumbnail);
next.setAbsPath(absPath);
galleryMedia.add(next);
}
return galleryMedia;
}
However I'm not sure if my media query is setup correctly and I'm definitely not sure how to determine whether the file is an image/photo of a video, which I (believe I) need so that I can use the correct method for obtaining the thumbnail Bitmap.
Can anyone help nudge me over the finish line here?
Here is a code I was using for something similar, I hope this will help:
//The code has been removed
Note: Sorry I've removed the code because I am not sure if it has some portion copied from other open-sourced codes, I am using it in my apps but I can't remember if it is quoted from other source, so I've removed it, also it could be auto-completed using github copilot
I have a list of images:
private int[] images = {
R.drawable.blue_icon_left_foot,
R.drawable.blue_icon_right_foot,
R.drawable.blue_icon_left_hand,
R.drawable.blue_icon_right_hand,
R.drawable.green_icon_left_foot,
R.drawable.green_icon_right_foot,
R.drawable.green_icon_left_hand,
R.drawable.green_icon_right_hand,
R.drawable.red_icon_left_foot,
R.drawable.red_icon_right_foot,
R.drawable.red_icon_left_hand,
R.drawable.red_icon_right_hand,
R.drawable.yellow_icon_left_foot,
R.drawable.yellow_icon_right_foot,
R.drawable.yellow_icon_left_hand,
R.drawable.yellow_icon_right_hand};
I want to let the user pick an image from the gallery by using an Intent:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
One of the images of the array should then be replaced with the new image given by the user. When I get the image from the user, I only have a URI of the image:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1) {
Uri imageUri = data.getData();
//Insert image into images list
}
}
Is it possible to somehow get an integer ID of the image so that I can insert the image into the same list as the images from the drawable folder?
Or should I instead try to store a list of URI's (if it is possible to get URI's of the images from the drawable folder)?
Or is there a third solution that is completely different and much better?
You can generate ids with View.generateViewId() if you are using API 17 or larger.
From the main documentation:
Generate a value suitable for use in setId(int). This value will not
collide with ID values generated at build time by aapt for R.id.
Returns a generated ID value
You can check this answer to see what are the alternatives when you are using a lower API: Android: View.setID(int id) programmatically - how to avoid ID conflicts?
I think what you are trying to do is somewhat misguided. The resources in the /res directory are compile time resources that you bundle with your project. The image that a person selects via an intent from their device is a run time file that will vary by user, device, etc. You are better off not trying to treat them the same. Save the user selections as dataUris or strings and keep the resources as ints.
I found this library "Image Gallery" and is very useful to my project.
I have two questions for you. If you can help me
the first one is about the files, how can put local files (stored in sd card) in the Arraylist . cause I put a list with local files ("/storage/emulated/0/APP_FILES/2015_09_15_033612.jpg") but seems not liked.
the second one is about the names of the pictures, if the library support adding names to the pics
This is a part of the Activity code if put URLS to web image files works but I need to use Local Files stored in SD CARD.
DonĀ“t work , so here is the code.
Intent intent = new Intent(MainActivity.this, ImageGalleryActivity.class);
ArrayList<String> images = new ArrayList<>();
images.add("/storage/emulated/0/APP_FILES/2015_09_15_033612.jpg");
images.add("/storage/emulated/0/APP_FILES/2015_09_15_03213321.jpg");
images.add("/storage/emulated/0/APP_FILES/2015_09_15_01234.jpg");
intent.putStringArrayListExtra("images", images);
// optionally set background color using Palette
intent.putExtra("palette_color_type", PaletteColorType.VIBRANT);
startActivity(intent);
the library is
https://github.com/lawloretienne/ImageGallery
If any one knows about another simple image library to implements names and images please advise me.
thanks in advance
The below method gave the answer of your both questions,
You should do like below,
Intent intent = new Intent(MainActivity.this, ImageGalleryActivity.class);
ArrayList<String> images = getImageList("/storage/emulated/0/APP_FILES/");
intent.putStringArrayListExtra("images", images);
// optionally set background color using Palette
intent.putExtra("palette_color_type", PaletteColorType.VIBRANT);
startActivity(intent);
private ArrayList<String> getImageList(String dirPath)
{
ArrayList<String> paths = new ArrayList<String>();
File[] listList = new File(path).listFiles();
for (int i = 0 ; i < listList.length; i++)
{
paths.add("file://"+listList[i]);
}
return paths;
}
Trying to read the list of photos taken by the user and display them in the imageview. For the sake of simplicity, trying to just display one for now. Have this piece of code so far:
Cursor cc = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,null);
which gets me some data in the cursor, cc.getCount() appears to be making sense (goes up by one when I take a picture, etc.). However, I cannot display the contents in the imageView at all, nothing ever shows up.
Iv'e tried this (s_id is the id of the pic from the cursor above, first returned column):
Uri u;
u = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + s_id);
im.setImageURI(u);
Also tried this:
Bitmap b = BitmapFactory.decodeFile(u.getPath());
im.setImageBitmap(b);
No worky. Help?
ps. no errors show up anywhere.
Here's another way you can let the user pick an image and display it in an imageview.
This code will allow the user to look through files to pick an image from the gallery:
Intent picture = new Intent(Intent.ACTION_GET_CONTENT);
picture.setType("image/*");
startActivityForResult(picture, YOUR_CODE);
Then in the onActivityResult method you can use the data to set the imageview:
public void onActivityResult(int requestCode, int resultCode, Intent data){
//This is where you can use the data from the previous intent to set the image view
Uri uri = data.getData();
im.setImageURI(uri); //where im is your imageview
}
Check out this link for a more detailed answer.
I'm working on an Android 2.1+ app and trying to take a photo and then send the results of that intent to a crop function. It seems to work on the Sony Ericsson xmp, but when i put it on a Moto Defy, the crop function fails because it is seemingly ignoring the file name i put in for the data, and instead looks at /data/data/com.motorola.gallery/files/temp-wallpaper for some reason. This has the result of the crop function not returning with any data, and the image i just took gets set as the wallpaper image of the phone!
Here's some sample code:
public static final String SD_CARD_TEMP_DIR = Environment.getExternalStorageDirectory() + File.separator;
public static final String SD_CARD_TEMP_ORIG = SD_CARD_TEMP_DIR + "origPhoto.jpg";
public static final String SD_CARD_TEMP_CROP = SD_CARD_TEMP_DIR + "croppedPhoto.jpg";
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(new File(Const.SD_CARD_TEMP_ORIG)), "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 512);
intent.putExtra("outputY", 512);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Const.SD_CARD_TEMP_CROP)));
The output from this is something like this:
INFO/ActivityManager(1242): Starting activity: Intent { act=com.android.camera.action.CROP dat=file:///sdcard/origPhoto.jpg typ=image/* cmp=com.motorola.gallery/.CropImage (has extras) }
INFO/ActivityManager(1242): Start proc com.motorola.gallery:CropImage for activity com.motorola.gallery/.CropImage: pid=25733 uid=10014 gids={1015, 9003, 9007, 9008, 2001, 3003}
WARN/CropImage(25733): CropImg mBitmap was null, retreive frm URIfile:///sdcard/origPhoto.jpg
ERROR/CropImage(25733): got IOException java.io.FileNotFoundException: /data/data/com.motorola.gallery/files/temp-wallpaper
INFO/ActivityManager(1242): Displayed activity com.motorola.gallery/.CropImage: 5664 ms (total 5664 ms)
WARN/System.err(25672): java.io.FileNotFoundException: /sdcard/croppedPhoto.jpg
DEBUG/(25672): unable to unlink '/sdcard/croppedPhoto.jpg': No such file or directory (errno=2)
Does anyone have any experience with this issue, or ideas for a work around?
I'd stay away from the Intent you're using, because it isn't standard, so may not be supported everywhere,
From the extra-data you're using, I believe you're doing a fixed-size crop, and don't need any user input. This makes the solution quite simple, as long as you don't run out of memory.
Load the image via BitmapFactory.decodeFile. You can pass in a BitmapFactory.options object to optionally scale the image at load time.
Create a cropped bitmap with Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)
Write it out to disk with Bitmap.compress