【问题标题】:Open gallery app from Android Intent从 Android Intent 打开图库应用
【发布时间】:2013-06-04 22:45:35
【问题描述】:

我正在寻找一种从意图打开 Android 画廊应用程序的方法。

我不想返回图片,而只是打开图库以允许用户使用它,就像他们从启动器中选择它一样 (View pictures/folders)。

我已尝试执行以下操作:

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_GET_CONTENT);  
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

但是,这会导致应用程序在我选择图片后关闭(我知道这是因为ACTION_GET_CONTENT),但我只需要打开图库。

任何帮助都会很棒。

谢谢

【问题讨论】:

标签: java android android-intent


【解决方案1】:

这是你需要的:

ACTION_VIEW

将您的代码更改为:

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);  
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

【讨论】:

  • 这是回答问题的唯一答案!他只需要我只需要打开画廊。不挑选或归还nada
  • 这个答案是我一直在寻找的,我也在我的一个应用程序中实现了相同的代码,但它在某些设备中崩溃:-(
  • 如何使用这个意图打开画廊中的图片文件夹,你能帮我吗。
【解决方案2】:

我希望这会对你有所帮助。这段代码对我有用。

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
 startActivityForResult(i, RESULT_LOAD_IMAGE);

ResultCode 用于将所选图像更新到图库视图

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);

        if (cursor == null || cursor.getCount() < 1) {
            return; // no cursor or no record. DO YOUR ERROR HANDLING
        }

        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

        if(columnIndex < 0) // no column index
            return; // DO YOUR ERROR HANDLING

        String picturePath = cursor.getString(columnIndex);

        cursor.close(); // close cursor

        photo = decodeFilePath(picturePath.toString());

        List<Bitmap> bitmap = new ArrayList<Bitmap>();
        bitmap.add(photo);
        ImageAdapter imageAdapter = new ImageAdapter(
                AddIncidentScreen.this, bitmap);
        imageAdapter.notifyDataSetChanged();
        newTagImage.setAdapter(imageAdapter);
    }

【讨论】:

  • 不应该关闭光标吗?
  • picturePath 在我的情况下为空(android 7.0)。
  • 无法导入 decodeFilePath()。使用 BitmapFactory.decodeFile(pathname) 代替。
【解决方案3】:

您可以使用以下意图打开图库:

public static final int RESULT_GALLERY = 0;

Intent galleryIntent = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , RESULT_GALLERY );

如果您想获取结果 URI 或做其他任何事情,请使用:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case QuestionEntryView.RESULT_GALLERY :
        if (null != data) {
            imageUri = data.getData();
            //Do whatever that you desire here. or leave this blank

        }
        break;
    default:
        break;
    }
}

在 Android 4.0+ 上测试

【讨论】:

  • 非常感谢你:)
【解决方案4】:
  1. Following 可用于 Activity 或 Fragment。

     private File mCurrentPhoto;
    
  2. 添加权限

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"  />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
    
  3. 添加 Intents 以打开“图像选择器”和“照片捕获”

    //A system-based view to select photos.
    private void dispatchPhotoSelectionIntent() {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    this.startActivityForResult(galleryIntent, REQUEST_IMAGE_SELECTOR);
    }
    
    
    
        //Open system camera application to capture a photo.
        private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(App.Instance.getPackageManager()) != null) {
        try {
            createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (mCurrentPhoto != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhoto));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
    }
    
  4. 获取照片时添加处理。

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQUEST_IMAGE_SELECTOR:
        if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = App.Instance.getContentResolver().query(data.getData(), filePathColumn, null, null, null);
            if (cursor == null || cursor.getCount() < 1) {
                mCurrentPhoto = null;
                break;
            }
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            if(columnIndex < 0) { // no column index
                mCurrentPhoto = null;
                break;
            }
            mCurrentPhoto = new File(cursor.getString(columnIndex));
            cursor.close();
        } else {
            mCurrentPhoto = null;
        }
        break;
    case REQUEST_IMAGE_CAPTURE:
        if (resultCode != Activity.RESULT_OK) {
            mCurrentPhoto = null;
        }
        break;
    }
    if (mCurrentPhoto != null) {
        ImageView imageView = (ImageView) [parent].findViewById(R.id.loaded_iv);
        Picasso.with(App.Instance).load(mCurrentPhoto).into(imageView);
    }
    super.onActivityResult(requestCode, resultCode, data);
    }
    

【讨论】:

  • 打开画廊真的需要权限吗?我无需使用任何权限即可打开图库。
  • @IsharaAmarasekera 如果没有崩溃并且一切正常,那么就不会
【解决方案5】:

由于您不希望返回结果,请尝试以下简单代码。

Intent i=new Intent(Intent.ACTION_PICK);
            i.setType("image/*");
            startActivity(i);

【讨论】:

    【解决方案6】:

    如果有人仍然收到错误,即使添加了以下代码

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

    那么您可能会在另一个类中打开意图(通过调用),因此当您单击按钮时应用程序将崩溃。

    问题的解决方案

    将intent放入连接到按钮布局文件的类文件中。 例如 - activity_main.xml 包含按钮,它连接到 MainActivity.java 文件。但是由于碎片化,您在其他一些类中定义意图(让我们说 Activity.java)然后您的应用程序将崩溃除非您将意图放在 MainActivity.java 文件中。我收到了这个错误并且就这样解决了。

    【讨论】:

      猜你喜欢
      • 2019-01-29
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多