从图库中选择的图像,但它不会显示在我的图像按钮中

时间:2014-08-14 14:58:38

标签: android image gallery

我正在创建一个功能,当我在警告对话框中选择“从图库中选择”时,图库中的所选图像将出现在图像按钮中。我不知道我的代码有什么问题。 我以这些网站为例http://geekonjava.blogspot.sg/2014/03/upload-image-on-server-in-android-using.htmlhttp://www.c-sharpcorner.com/UploadFile/e14021/capture-image-from-camera-and-selecting-image-from-gallery-o/。没有任何代码错误或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"/>

2 个答案:

答案 0 :(得分:0)

尝试使图片按钮无效。这样你告诉UI应该重绘它,因为值已经改变了。

答案 1 :(得分:0)

尝试将onActivityResult更改为

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

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