Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.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_Gallery - Fatal编程技术网

Android 已从库中选择图像,但它不显示在“我的图像”按钮中

Android 已从库中选择图像,但它不显示在“我的图像”按钮中,android,image,gallery,Android,Image,Gallery,我正在制作一个功能,当我在警报对话框中选择“从图库中选择”时,图库中选定的图像将显示在imagebutton中。我不知道我的代码出了什么问题。 我以这些网站为例来做这件事。没有任何代码错误或logcat错误。有人能帮我吗 这是我的密码: public static class CardFrontFragment extends Fragment implements OnClickListener{ private int serverResponseCode = 0;

我正在制作一个功能,当我在警报对话框中选择“从图库中选择”时,图库中选定的图像将显示在imagebutton中。我不知道我的代码出了什么问题。 我以这些网站为例来做这件事。没有任何代码错误或logcat错误。有人能帮我吗

这是我的密码:

public static class CardFrontFragment extends Fragment implements OnClickListener{

        private int serverResponseCode = 0;
    private ProgressDialog dialog = null;
    private String uploadServerUri = null;
    private String imagepath = null;
    private ImageButton upload;


    public CardFrontFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        RelativeLayout layout = (RelativeLayout) inflater.inflate(
                R.layout.card_front, container, false);

         upload = (ImageButton) layout.findViewById(R.id.fileUpload);

        upload.setOnClickListener(this);
        uploadServerUri = Constants.serverUrl + "api/FileUpload";

        return layout;
    }

    @Override
    public void onClick(View arg0) {
        if(arg0 == upload)
        {

            selectImage();

        }

    } 

     private void selectImage() {

            final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle("Add Photo!");
            builder.setItems(options, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    if (options[item].equals("Take Photo"))
                    {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                        startActivityForResult(intent, 1);
                    }
                    else if (options[item].equals("Choose from Gallery"))
                    {
                        Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(intent, 2);

                    }
                    else if (options[item].equals("Cancel")) {
                        dialog.dismiss();
                    }
                }
            });
            builder.show();
        }

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

        if (requestCode == 2 && resultCode == RESULT_OK) {
            //Bitmap photo = (Bitmap) data.getData().getPath(); 

            Uri selectedImageUri = data.getData();
            imagepath = getPath(selectedImageUri);
            Bitmap bitmap=BitmapFactory.decodeFile(imagepath);
            upload.setImageBitmap(bitmap);

        }
    }   

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

}
以下是我在androidmanifest.xml中声明的权限:

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


尝试使imagebutton无效。这样,您可以告诉UI应该重新绘制,因为值已更改。

尝试将您的
onActivityResult
更改为

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

    if (requestCode == 1 && resultCode == RESULT_OK) {
        Uri selectedImageUri = data.getData();
        upload.setImageURI(selectedImageUri);
    }
}   

尝试记录
selectedImageUri
imagepath
变量。这些变量的值是什么?这就是你想说的吗upload=(ImageButton)layout.findViewById(R.id.fileUpload);upload.invalidate();'是的,invalidate是告诉视图某些内容已更改,需要重新绘制。试试看,让我知道进展如何。这里有一些关于invalidate==>(搜索invalidate)的更多信息。是的,我试过这个upload=(ImageButton)layout.findViewById(R.id.fileUpload);upload.invalidate();'它不起作用:(Owh:(我将尝试在一个小示例应用程序中重新创建您的项目,亲自看看,我会让您不断更新。如果您找到解决方案,也请让我知道:)我尝试将所有代码放在片段之外。并且从库中选择的图像能够显示。因此,我猜问题在于片段。