怎么打开画廊?

时间:2013-10-19 14:30:41

标签: android android-activity

我需要通过我的应用程序中的代码打开图像库。(仅打开图库,用户不会选择任何图像)。我搜索并发现了许多方法,但其中一些仅用于选择图像和其他从未工作的方式。

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

我怎么能打开画廊?

1 个答案:

答案 0 :(得分: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或将其声明为全局变量