上传完成后,无法在图像视图上设置图像

时间:2014-11-12 10:30:13

标签: android

public class FragProfile extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.frag_profile, container, false);
        Profile_image = (ImageView) root.findViewById(R.id.frag_imgProfile);
        texusername = (TextView) root.findViewById(R.id.textView);
        editbutton = (ImageView) root.findViewById(R.id.imageView1);
        textcityname = (TextView) root.findViewById(R.id.textView1);

        // ((HomeActivity) getActivity()).notifcationbadge
        // .setVisibility(View.GONE);
        // ((HomeActivity) getActivity()).badgeicon.setVisibility(View.GONE);

        // ((HomeActivity) getActivity()).EditProfileimage
        // .setVisibility(View.VISIBLE);
        ((HomeActivity) getActivity()).posttext.setVisibility(View.GONE);
        editbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent editnameandcity = new Intent(getActivity(),
                        EditnameandCity.class);
                startActivity(editnameandcity);

            }
        });

        SharedPreferences preferences = getActivity().getSharedPreferences(
                AppConstants.LOGIN_PREFS, Context.MODE_PRIVATE);
        member_id = preferences.getString("Member_id", "");
        // member_id = "409";
        image_Url = "http://api.lociiapp.com/TransientStorage/" + member_id
                + ".jpg";
        Profile_image.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                final String[] items = new String[] { "Take from camera",
                        "Select from gallery" };
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        getActivity(), android.R.layout.select_dialog_item,
                        items);
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        getActivity());

                builder.setTitle("Select Image");
                builder.setAdapter(adapter,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int item) { // pick
                                                                                    // from
                                                                                    // camera
                                if (item == 0) {
                                    Intent intent = new Intent(
                                            MediaStore.ACTION_IMAGE_CAPTURE);

                                    mImageCaptureUri = Uri.fromFile(new File(
                                            Environment
                                                    .getExternalStorageDirectory(),
                                            "tmp_avatar_"
                                                    + String.valueOf(System
                                                            .currentTimeMillis())
                                                    + ".jpg"));

                                    intent.putExtra(
                                            android.provider.MediaStore.EXTRA_OUTPUT,
                                            mImageCaptureUri);

                                    try {
                                        intent.putExtra("return-data", true);

                                        startActivityForResult(intent,
                                                PICK_FROM_CAMERA);
                                    } catch (ActivityNotFoundException e) {
                                        e.printStackTrace();
                                    }
                                } else { // pick from file
                                    Intent intent = new Intent(
                                            Intent.ACTION_PICK,
                                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                                    intent.setType("image/*");
                                    intent.setAction(Intent.ACTION_GET_CONTENT);

                                    startActivityForResult(Intent
                                            .createChooser(intent,
                                                    "Complete action using"),
                                            PICK_FROM_FILE);
                                }
                            }
                        });
                final AlertDialog dialog = builder.create();
                dialog.show();
                return false;

            }

        });

        return root;

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK)
            return;

        switch (requestCode) {
        case PICK_FROM_CAMERA:
            doCrop();

            break;

        case PICK_FROM_FILE:
            mImageCaptureUri = data.getData();

            doCrop();

            break;

        case CROP_FROM_CAMERA:
            Bundle extras = data.getExtras();

            if (extras != null) {
                Bitmap photo = extras.getParcelable("data");

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.PNG, 90, stream);

                byte[] byte_arr = stream.toByteArray();

                String image_str = Base64.encodeToString(byte_arr,
                        Base64.DEFAULT);

                new Uploadimage().execute(image_str);

            }

            File f = new File(mImageCaptureUri.getPath());

            if (f.exists())
                f.delete();

            break;

        }
    }

    private void doCrop() {
        final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setType("image/*");

        List<ResolveInfo> list = getActivity().getPackageManager()
                .queryIntentActivities(intent, 0);

        int size = list.size();

        if (size == 0) {
            Toast.makeText(getActivity(), "Can not find image crop app",
                    Toast.LENGTH_SHORT).show();

            return;
        } else {
            intent.setData(mImageCaptureUri);

            intent.putExtra("outputX", 50);
            intent.putExtra("outputY", 50);
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("scale", true);
            intent.putExtra("return-data", true);

            if (size == 1) {
                Intent i = new Intent(intent);
                ResolveInfo res = list.get(0);

                i.setComponent(new ComponentName(res.activityInfo.packageName,
                        res.activityInfo.name));

                startActivityForResult(i, CROP_FROM_CAMERA);
            } else {
                for (ResolveInfo res : list) {
                    final CropOption co = new CropOption();

                    co.title = getActivity().getPackageManager()
                            .getApplicationLabel(
                                    res.activityInfo.applicationInfo);
                    co.icon = getActivity().getPackageManager()
                            .getApplicationIcon(
                                    res.activityInfo.applicationInfo);
                    co.appIntent = new Intent(intent);

                    co.appIntent
                            .setComponent(new ComponentName(
                                    res.activityInfo.packageName,
                                    res.activityInfo.name));

                    cropOptions.add(co);
                }

                CropOptionAdapter adapter = new CropOptionAdapter(
                        getActivity(), cropOptions);

                AlertDialog.Builder builder = new AlertDialog.Builder(
                        getActivity());
                builder.setTitle("Choose Crop App");
                builder.setAdapter(adapter,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int item) {
                                startActivityForResult(
                                        cropOptions.get(item).appIntent,
                                        CROP_FROM_CAMERA);
                            }
                        });

                builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {

                        if (mImageCaptureUri != null) {
                            getActivity().getContentResolver().delete(
                                    mImageCaptureUri, null, null);
                            mImageCaptureUri = null;
                        }
                    }
                });

                AlertDialog alert = builder.create();

                alert.show();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        new username().execute();
//      MtDrawableManager.getInstance().fetchBitmapOnThread(image_Url,
//              Profile_image);

        AQuery aq = new AQuery(context);
        aq.id(Profile_image).image(image_Url, true, false, 0,
                R.drawable.imge);

    }

    class Uploadimage extends AsyncTask<String, Void, Void> {
        InputStream inputStream;
        private static final String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setTitle("Updating...");
            pDialog.setMessage("Please wait...");
            pDialog.setCanceledOnTouchOutside(false);
            pDialog.show();
        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                String Url = Uri.encode(
                        "http://api.lociiapp.com/api/registration/ProfilePictureSaved?imageFile="
                                + params[0] + "&member_id=" + member_id
                                + "&picture_path=" + member_id + ".jpg",
                        ALLOWED_URI_CHARS);

                HttpClient httpclient = new DefaultHttpClient();
                HttpGet httppost = new HttpGet(Url);

                httppost.setHeader(
                        "Accept",
                        "text/html,application/xhtml+xml,application/xml,application/json;q=0.9,/;q=0.8");
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity);
                System.out.println("15aug " + result);
            } catch (Exception e) {
                pDialog.dismiss();
                pDialog.cancel();
                System.out.println("Error in http connection " + e.toString());
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            pDialog.cancel();

        //  MtDrawableManager.getInstance().fetchBitmapOnThread(image_Url,
        //          Profile_image);
            AQuery aq = new AQuery(context);
            aq.id(Profile_image).image(image_Url, true, false, 0,
                    R.drawable.imge);

            Log.e("Image Url**************", image_Url);

        }

    }

这是我的代码,用于将图像上传到服务器使用相机和图库。我能够在两种情况下成功上传图像但是有Profile_image是图像视图,我在上传到服务器后打印该图像我在postexcute方法中正确获取image_url但是它在图像上显示图像Profile_image请告诉我在哪里做的我即使在简历方法中也能找到问题我已经为显示图像推出了相同的代码。

1 个答案:

答案 0 :(得分:0)

您可以通过图片加载器尝试图像加载。 通过以下链接下载图像加载器。 http://www.androidhive.info/2012/07/android-loading-image-from-url-http/