2

嗨,我想仅使用 android 中的内置图库打开图像。现在我正在使用以下代码,如果我单击按钮,它会显示包含安装的第三方工具以打开图像的菜单。我只想要内置画廊,有没有隐藏其他第三方工具的选项,我可以直接用画廊打开而不显示菜单。

package com.example.gallery;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BrowsePicture extends Activity {

private static final int SELECT_PICTURE = 1;
Button bt;
private String selectedImagePath;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_browse_picture);

    bt = (Button) findViewById(R.id.button1);
            bt.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);
        }
    }
}

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

}
4

2 回答 2

6

尝试使用

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PICTURE);
于 2013-01-18T08:33:52.787 回答
1

那是因为单击按钮时,您正在传递设置其类型的意图, intent.setType("image/*");因此intent.setAction(Intent.ACTION_GET_CONTENT);它将显示设备上安装的应用程序列表。

如果您只想直接打开画廊,那么您应该只传递画廊的意图。

于 2013-01-18T07:37:26.997 回答