Android の組み込みギャラリー アプリからプログラムで画像を取得/選択する 質問する

Android の組み込みギャラリー アプリからプログラムで画像を取得/選択する 質問する

アプリケーション内からギャラリー組み込みアプリで画像/写真を開こうとしています。

画像の URI があります (画像は SD カードにあります)。

何か提案はありますか?

ベストアンサー1

これは完全な解決策です。@mad が下記の回答で提供した情報を使用して、このサンプル コードを更新しました。また、picasa イメージの処理方法を説明している @Khobaib の下記の解決策も確認してください。

アップデート

私は元の回答を確認し、github からチェックアウトしてシステムに直接インポートできるシンプルな Android Studio プロジェクトを作成しました。

https://github.com/hanscappelle/SO-2169649

(複数のファイルの選択にはまだ改善の余地があることにご注意ください)

単一画像の選択

ユーザー mad のおかげで、ファイル エクスプローラーからの画像がサポートされました。

public class BrowsePictureActivity extends Activity {

    // this is the action code we use in our intent, 
    // this way we know we're looking at the response from our own action
    private static final int SELECT_PICTURE = 1;

    private String selectedImagePath;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.Button01)
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {

                        // in onCreate or any event where your want the user to
                        // select a file
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent,
                                "Select Picture"), SELECT_PICTURE);
                    }
                });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
            }
        }
    }

    /**
     * helper to retrieve the path of an image URI
     */
    public String getPath(Uri uri) {
            // just some safety built in 
            if( uri == null ) {
                // TODO perform some logging or show user feedback
                return null;
            }
            // try to retrieve the image from the media store first
            // this will only work for images selected from gallery
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            if( cursor != null ){
                int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                String path = cursor.getString(column_index);
                cursor.close();
                return path;
            }
            // this is our fallback here
            return uri.getPath();
    }

}

複数の画像を選択する

誰かがコメントでその情報を要求したので、情報を収集しておいた方が良いでしょう。

EXTRA_ALLOW_MULTIPLEインテントに追加のパラメータを設定します。

intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

結果処理でそのパラメータをチェックします。

if (Intent.ACTION_SEND_MULTIPLE.equals(data.getAction()))
        && Intent.hasExtra(Intent.EXTRA_STREAM)) {
    // retrieve a collection of selected images
    ArrayList<Parcelable> list = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
    // iterate over these images
    if( list != null ) {
       for (Parcelable parcel : list) {
         Uri uri = (Uri) parcel;
         // TODO handle the images one by one here
       }
   }
} 

これは API レベル 18 以上でのみサポートされていることに注意してください。

おすすめ記事