开发者

Accessing Android native gallery via an intent

开发者 https://www.devze.com 2023-02-09 05:39 出处:网络
How can I get a user to select from the native gallery in Android, rather than other gallery-like applications such as ASTRO File Manager?

How can I get a user to select from the native gallery in Android, rather than other gallery-like applications such as ASTRO File Manager?

The following code gives a list of activities that can select an image:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
List<ResolveInfo> infos = activity.getPackageManager().queryIntentActivities(intent, 0);

If I then do something like this:

activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_CHOOSE_IMAGE);

then if the user has more than one application that can 开发者_Go百科act like a gallery (such as ASTRO File Manager), s/he is prompted to select one of them, with no "set as default" option.

I don't want the user to be prompted to choose between them each time, so I'd like to just use the native gallery.

The hacky code sample below uses a whitelist to test for known native gallery names:

    for (ResolveInfo info : infos) {
        if (    0==info.activityInfo.name.compareTo("com.cooliris.media.Gallery")
            ||  0==info.activityInfo.name.compareTo("com.htc.album.CollectionsActivity")
        ) {
            // found the native gallery
            doSomethingWithNativeGallery();
        }
    }

Feels kinda dirty. Is there a better way? I suspect I'm missing something in my intent.


I didn't get last part of your code.

To start the native gallry what i did is -

    public void upload(){
          Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
          photoPickerIntent.setType("image/jpg");
          photoPickerIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/image.jpg"));
          startActivityForResult(photoPickerIntent, 1);

   }

Call the upload() where you like to start the native gallery.

Then to get that image info i did -

     /**
 * Retrieves the returned image from the Intent, inserts it into the MediaStore, which
 *  automatically saves a thumbnail. Then assigns the thumbnail to the ImageView.
 *  @param requestCode is the sub-activity code
 *  @param resultCode specifies whether the activity was cancelled or not
 *  @param intent is the data packet passed back from the sub-activity
 */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (resultCode == RESULT_CANCELED) {
        return;
    }
    else if(resultCode == RESULT_OK) {
        Uri uri = intent.getData();

        String path = getPath(uri);
        Log.i("PATH", path);
        data = path;

                    return;
   }
   uplad();
}



   public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

If you wouldn't get your answer, clear what you like to do

0

精彩评论

暂无评论...
验证码 换一张
取 消