Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.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
Android 从映像中选择映像并获取返回的目录_Android_Image_Directory_Gallery - Fatal编程技术网

Android 从映像中选择映像并获取返回的目录

Android 从映像中选择映像并获取返回的目录,android,image,directory,gallery,Android,Image,Directory,Gallery,我必须启动Android的图像库,让用户选择图像。所以我希望在选择之后,返回目录映像 我怎么做 谢谢。米歇尔 要从库中启动图像选择的Intent,请使用以下代码: public void imageFromGallery() { Intent getImageFromGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI); startAc

我必须启动Android的图像库,让用户选择图像。所以我希望在选择之后,返回目录映像

我怎么做

谢谢。

米歇尔

要从库中启动图像选择的
Intent
,请使用以下代码:

public void imageFromGallery() {
    Intent getImageFromGalleryIntent = 
      new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(getImageFromGalleryIntent, SELECT_IMAGE);
}
然后,一旦用户做出选择,您将在
onActivityResult()中得到结果,如下所示:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch(requestCode) {
            case SELECT_IMAGE:
                String imagePath = getPath(data.getData());
                break;
        }
    }

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
getPath()
是从返回的URI对象获取路径的函数。这将返回一个带有所需路径的
字符串

干杯

米歇尔

要从库中启动图像选择的
Intent
,请使用以下代码:

public void imageFromGallery() {
    Intent getImageFromGalleryIntent = 
      new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(getImageFromGalleryIntent, SELECT_IMAGE);
}
然后,一旦用户做出选择,您将在
onActivityResult()中得到结果,如下所示:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch(requestCode) {
            case SELECT_IMAGE:
                String imagePath = getPath(data.getData());
                break;
        }
    }

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
getPath()
是从返回的URI对象获取路径的函数。这将返回一个带有所需路径的
字符串

干杯