Reading user photos in android - java

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.

Related

Querying Android ContentResolver for gallery files and determining image vs video

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

Android: open video picker and filter videos seen by length

This is my code for opening up the picker for Videos and Images - however I need to not show Videos that are longer than 5 minutes in length. Is this possible?
public void startChoosePhotoFromLibrary() {
if (checkOrRequestExternalStoreagePermission()) {
if (Build.VERSION.SDK_INT < 19) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/* video/*");
startActivityForResult(photoPickerIntent, PICK_PHOTO_ACTIVITY_REQUEST_CODE);
} else {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("*/*");
photoPickerIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"});
startActivityForResult(photoPickerIntent, PICK_PHOTO_ACTIVITY_REQUEST_CODE);
}
}
}
This is my code for opening up the picker for Videos and Images
ACTION_PICK does not use MIME types. ACTION_PICK picks from a collection of content, where that collection is identified by the Uri that you supply in the Intent.
Also, MIME types do not have spaces in them.
Is this possible?
Not via those Intent actions, or via any content-selection mechanism that is part of the Android SDK.
You are welcome to query the MediaStore for videos, and there may be a way to filter such videos by length. But then you would need to present your own UI for allowing the user to choose something from the query results (e.g., ListView, RecyclerView).

Pass image via intent to default contacts activity

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

Array with images from drawable and intent

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.

Image Gallery with support names and pics

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

Categories

Resources