Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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_Selector - Fatal编程技术网

Android-如何创建图像选择器

Android-如何创建图像选择器,android,image,selector,Android,Image,Selector,在我的应用程序中,我需要使用一些可以让用户从设备中选择图像的东西。 简单的方法是什么 记住,我将在选择后使用该图像 提前感谢。您可以尝试以下打开图库并让用户选择图像的方法 Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, ACTIVITY_SELECT_IMAGE);

在我的应用程序中,我需要使用一些可以让用户从设备中选择图像的东西。
简单的方法是什么

记住,我将在选择后使用该图像


提前感谢。

您可以尝试以下打开图库并让用户选择图像的方法

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

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

switch(requestCode) { 
case REQ_CODE_PICK_IMAGE:
    if(resultCode == RESULT_OK){  
        Uri selectedImage = imageReturnedIntent.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();


        Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
    }
}
}


“selectedImage”是选定的图像,因此您现在可以在应用程序的其余部分中使用它

您可以尝试以下操作,打开图库并让用户选择图像

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

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

switch(requestCode) { 
case REQ_CODE_PICK_IMAGE:
    if(resultCode == RESULT_OK){  
        Uri selectedImage = imageReturnedIntent.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();


        Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
    }
}
}


“selectedImage”是选定的图像,因此您现在可以在应用程序的其余部分中使用它

使用此代码从库中选择图像

galleryPic.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            Util.DogDye = false;
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(
                    Intent.createChooser(intent, "Complete action using"),
                    1);

        }
    });
然后添加一个新方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if (resultCode != RESULT_OK) return;

    switch (requestCode) {

    case 1:
        if (data != null) {
            selectedImageUri = data.getData();
            String selectedImagePath = getPath(path);
BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
Bitmap btemp = BitmapFactory.decodeFile(selectedImagePath,
                        options);
          /// use btemp Image file 

        }
        break;
    }

}


   public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

使用此代码从库中选择图像

galleryPic.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            Util.DogDye = false;
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(
                    Intent.createChooser(intent, "Complete action using"),
                    1);

        }
    });
然后添加一个新方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if (resultCode != RESULT_OK) return;

    switch (requestCode) {

    case 1:
        if (data != null) {
            selectedImageUri = data.getData();
            String selectedImagePath = getPath(path);
BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
Bitmap btemp = BitmapFactory.decodeFile(selectedImagePath,
                        options);
          /// use btemp Image file 

        }
        break;
    }

}


   public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

用户要从gallery中选择图像吗?用户要从gallery中选择图像吗?没问题,很高兴听到=)没问题,很高兴听到=)