Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
setType()返回Android中所有类型的文件_Android_Android Intent - Fatal编程技术网

setType()返回Android中所有类型的文件

setType()返回Android中所有类型的文件,android,android-intent,Android,Android Intent,我试图通过intent从不同的应用程序中获取图片 我正在使用以下代码: Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.addCategory("android.intent.category.OPENABLE"); intent.setType("image/*"); 有趣的是,这在Google Nexus 7 Android 4.4上运行良好,但对于其他设备(Andro

我试图通过intent从不同的应用程序中获取图片

我正在使用以下代码:

    Intent intent = new Intent("android.intent.action.GET_CONTENT");

    intent.addCategory("android.intent.category.OPENABLE");
    intent.setType("image/*");
有趣的是,这在Google Nexus 7 Android 4.4上运行良好,但对于其他设备(Android 4.2.2),它允许我选择各种文件,如视频和.docx

为什么会这样

编辑
要明确的是,我所期望的是,它应该限制我选择图像类型以外的文件。但这并没有发生。

以下是我发现的对我有用的东西:

final static int IMPORT_PICTURE = 10001;
Intent intent = new Intent();
intent.setType("image/*"); //For specific type add image/jpeg
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), IMPORT_PICTURE);
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class StackOverflowAppActivity extends Activity {
    private final int PICK_IMAGE = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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


    }

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

        switch (requestCode) {

        case PICK_IMAGE:
            if (resultCode == RESULT_OK) {
                // do your thing
            }
        }
    }
}

这是一个来自

的解决方案,以下是我发现的对我有效的方法:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class StackOverflowAppActivity extends Activity {
    private final int PICK_IMAGE = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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


    }

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

        switch (requestCode) {

        case PICK_IMAGE:
            if (resultCode == RESULT_OK) {
                // do your thing
            }
        }
    }
}

这是由

提供的解决方案,感谢您的回复!但我很想知道为什么它在不同的设备上表现不同。您知道上述两台设备的设备配置有什么变化吗?谢谢您的回复!但我很想知道为什么它在不同的设备上表现不同。您知道上述两台设备的设备配置有任何变化吗?