0

我需要通过我的应用程序中的代码打开图片库。(只打开图库,用户不会选择任何图片)。我搜索并找到了很多方法,但其中一些仅用于选择图像而其他方法从未起作用。例如

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);

或者

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
 "content://media/internal/images/media")); 
 startActivity(intent); 

我怎样才能打开画廊?

4

1 回答 1

2
public void pickPhoto(View view) 
    {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
    }

这里:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
             Uri curImageURI = data.getData();
             Bitmap bit = getRealPathFromURI(curImageURI);
             imageView.setImageBitmap(bit);
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }

public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        android.database.Cursor cursor = managedQuery(contentUri, proj, null,
                null, null);
        int column_index;
        try {
            column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } catch (Exception e) {

            return null;
        }

    }

CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE 是 "startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);" 中的最终数字 在本例中为 '1',因此您需要将其更改为 1 或将其声明为全局变量

于 2013-10-19T16:06:14.683 回答