Mike
Mike

Reputation: 2409

Android receive images from Gallery in order

I currently have an Android application that has an intent-filter to receive images from the Gallery. It is important that the images are received in the same order that the user selected them in. This seems to be the default behavior on most devices, however on some devices (so far I've seen this on Motorola's running Android 4.x) the order seems undefined. Does anyone know a way to declare in the intent that the images should be received in order? Or a way once the images are recieved to determine the selected order?

Here is relevant code from the manifest

 <activity
       android:label="@string/app_name"
       android:name=".activities.ImportImagesActivity" > 
       <intent-filter>   
           <action android:name="android.intent.action.SEND" />
           <category android:name="android.intent.category.DEFAULT" /> 
           <data android:mimeType="image/*" />
       </intent-filter>
       <intent-filter>   
           <action android:name="android.intent.action.SEND_MULTIPLE" />
           <category android:name="android.intent.category.DEFAULT" /> 
           <data android:mimeType="image/*" />
       </intent-filter>
   </activity>

And from ImportImagesActivity

private List<Uri> parseIncomingData() {
    List<Uri> uriList = null;

    Intent intent = this.getIntent();
    if(intent != null) {
        String action = intent.getAction();
        //Single Image
        if (action.equalsIgnoreCase("android.intent.action.SEND")) {
            //removed for brevity
            }
        }
        //Multiple Images
        else if (action.equalsIgnoreCase("android.intent.action.SEND_MULTIPLE")) {
            uriList  = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
        }
    }

    //more code - at this point images have been recieved

    return uriList;
}

EDIT

To give a little context, let me explain the general flow of the app. The user opens the Gallery and selects images. They choose to 'Share' them with my app. My application receives a list of Uri's which are then displayed using an internal gallery backed by a custom Adapter class. The images display correctly based on the Uri list, the issue is the order of the List<Uri> is sometimes incorrect. It is important to my users that the images appear in the same order they select them in.

Clarification When I use the term Gallery I am referring to the built in Android app Gallery. When I use the term 'Share' I am referring to the the Share button within the Gallery app. This allows the user to select from a list of services such as Facebook, Email, and in this case my app.

For Example imagine a Gallery with 3 images, displayed in an arbitrary order: A, B, and C. The user selects first C then A then B and chooses to share them with my app. On most phones my list will be correctly ordered {C, A, B}, on offending phones this order seems random.

I cannot use the creation timestamp because the creation time is generally irrelevant to the selection order. Adding custom meta data doesn't help either because I don't know the correct initial order.

Upvotes: 3

Views: 2293

Answers (2)

Karthik Balakrishnan
Karthik Balakrishnan

Reputation: 4383

My observation is that Android gallery displays images in accordance to their recency.

For the devices where you're unable to determine the order, you can import the images from the gallery and check their creation time. Here's a way to do that. Or you could use a metadata extractor app, many jars can be found.

Now, you could just arrange the images in the order of recency and you should be done.

[EDIT]

I have a question. You said they may be selected in any order, so are they "uploading" it onto a server by "sharing"?

If so, then one way is to check which image was uploaded or if you want the order of selection, you could do this. Edit the metadata of the images, there's bound to be a useless tag, select one and edit it on touch. So, if I select image A it changes to 1 and then I select image B it becomes 2. But if I unselect image A now then image B should become 1. So, you could use nodes here. This is the first in first out (FIFO) method. Upon un-selection, A is thrown out of the list and B replaces it.

Is this what you wanted?

EDIT Sorry, I don't think you can do this without creating your own gallery. Why don't you just import the android gallery into a grid view in your app?

Upvotes: 1

GOLDEE
GOLDEE

Reputation: 2318

Yeah, I just faced the same problem right now. I am also using Fedor's lazy loading concept.I am not sure how far this will be helpful and whether this is the right approach. But still it solved the problem for me.

I had to do a little modification in the getView(),

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    System.gc();

    ImageView view;

    if(convertView == null) {

        view = new ImageView(context);

        view.setScaleType(ImageView.ScaleType.FIT_CENTER);
        view.setLayoutParams(new GridView.LayoutParams(screenWidth/4, screenHeight/4));
        view.setAdjustViewBounds(false);
        view.setPadding(2, 2, 2, 2);



        System.gc();

    }else {
        view = (ImageView) convertView;
    }
      if(view!=null)
        {
               imageLoader.DisplayImage(urlList.get(position), view);
               notifyDataSetChanged();  //Calling this helped to solve the problem. 
        }

    System.gc();

    return view;
}

Upvotes: 1

Related Questions