簡體   English   中英

在另一個類中未調用onActivityResult()

[英]onActivityResult() is not called in another class

我想要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;
    }

}

您的onActivityResult()應該位於FragmentActivity中,因為您僅從該類調用startActivityResult()

onActivityResult()僅在活動類中起作用。 首先,您擴展Activity,然后在onCreate()之后使用onActivityResult()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM