onActivityResult()不在另一个类中调用

时间:2014-04-03 09:20:07

标签: java android

我想startActivityResult()onActivityResult()未被调用...

在我的片段中:

    ivAvatar.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    ChangeAvatar.onClickAvatar(getActivity(), ivAvatar);
                    pathAvatar = ChangeAvatar.getPathAvatar();

                }
            });

在我的班级ChangeAvatar.java中:

    public class ChangeAvatar {

    public static FragmentActivity activity;
    private static Uri mCapturedImageURI;
    private static ImageView ivAvatar;
    private static String pathAvatar;

    private static final int CAPTURE_PICTURE = 0;
    private static final int SELECT_PICTURE = 1;
    private static final int SELECT_PICTURE_GOOGLE = 2;

    public ChangeAvatar () {

    }

    public static void onClickAvatar(FragmentActivity a, ImageView iv) {

        ivAvatar = iv;
        activity = a;

        final CharSequence[] items = { "Prendre une photo", "Choisir une image", "Annuler" };

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        // set title
        alertDialogBuilder.setTitle("Avatar :");
        // set dialog message
        alertDialogBuilder.setCancelable(false).setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {

                if (item == 0) {
                    ContentValues values = new ContentValues();
                    values.put(MediaStore.Images.Media.TITLE, "");
                    mCapturedImageURI = activity.getContentResolver().insert(
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                    intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
                    intent.putExtra("crop", "true");
                    intent.putExtra("aspectX", 170);
                    intent.putExtra("aspectY", 170);
                    intent.putExtra("outputX", 5000);
                    intent.putExtra("outputY", 5000);
                    activity.startActivityForResult(Intent.createChooser(intent, "Appareil photo"), CAPTURE_PICTURE);
                }

                if (item == 1) {
                    Intent intent = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    activity.startActivityForResult(Intent.createChooser(intent, "Choisir une application"),
                            SELECT_PICTURE);
                }

                if (item == 2) {
                    dialog.cancel();
                }

            }
        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // afficher
        alertDialogBuilder.show();

    }

    /**
     * Méthode qui permet d'ouvrir la gallery d'images ou l'appareil photo
     * 
     * @param requestCode CAPTURE ou SELECT
     * @param resultCode RESULT_OK or not
     * @param data Intent
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CAPTURE_PICTURE) {

                try {
                    ivAvatar.setImageBitmap(decodeUri(activity.getApplicationContext(), mCapturedImageURI, 85));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                pathAvatar = getRealPathFromURI(mCapturedImageURI);
                Log.i("TAG", pathAvatar + " : chemin");

            }

            if (requestCode == SELECT_PICTURE) {

                Uri selectedimg = data.getData();

                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setType("image/*");
                intent.setData(selectedimg);
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 170);
                intent.putExtra("aspectY", 170);
                intent.putExtra("outputX", 5000);
                intent.putExtra("outputY", 5000);
                intent.putExtra("return-data", true);
                activity.startActivityForResult(intent, SELECT_PICTURE_GOOGLE);

            }

            if (requestCode == SELECT_PICTURE_GOOGLE) {

                Bundle extras = data.getExtras();
                Bitmap mSelectedImage = extras.getParcelable("data");
                Uri selectedimg = getImageUri(mSelectedImage);

                try {
                    ivAvatar.setImageBitmap(Bitmap.createScaledBitmap(
                            decodeUri(activity.getApplicationContext(), selectedimg, 85), 170, 170, false));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                pathAvatar = getRealPathFromURI(selectedimg);
                Log.i("TAG", pathAvatar + " : chemin");
            }
        }
    }

    /**
     * Méthode qui retourne un bitmap compressé
     * 
     * @param c Context de l'application
     * @param uri Photo capturée
     * @param requiredSize Taille requise
     * @return Photo convertie en Bitmap
     * @throws FileNotFoundException Fichier non trouvé
     */
    public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize) throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);

        int widthTmp = o.outWidth, heightTmp = o.outHeight;
        int scale = 1;

        while (true) {
            if (widthTmp / 2 < requiredSize || heightTmp / 2 < requiredSize) {
                break;
            }
            widthTmp /= 2;
            heightTmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
    }

    /**
     * Méthode qui retourne le chemin du fichier
     * 
     * @param contentUri Uri de l'image
     * @return Chemin de l'Uri
     */
    public String getRealPathFromURI(Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = activity.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    /**
     * Méthode qui retourne l'Uri d'un Bitmap
     * 
     * @param inImage Bitmap
     * @return Uri
     */
    public Uri getImageUri(Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = Images.Media.insertImage(activity.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    }

    /**
     * Méthode qui retourne le chemin de l'avatar
     * 
     * @return pathAvatar
     */
    public static String getPathAvatar() {
        return pathAvatar;
    }

}

2 个答案:

答案 0 :(得分:1)

您的onActivityResult()应该位于FragmentActivity中,因为您只是从该类调用startActivityResult()

答案 1 :(得分:-1)

onActivityResult()仅适用于Activity Class。首先,在onCreate()之后使用onActivityResult()之后扩展Activity。