来自android.app.Fragment的OnActivityResult没有返回

时间:2014-10-21 14:36:42

标签: android android-fragments

我一直在寻找这个问题的日子,但一无所获 问题是当我从一个试图从图库中获取图像的片段调用onActivityResult()时 我选择照片后应用程序才关闭 我试过了 1-致电super.onActivityResult 2- this.onActivityResult 3-在我的活动中实施onActivityResult 我几乎没有尝试过每一个解决方案谷歌和stackoverflow介绍给我 请帮我 这是我的代码

package com.optimalsolutionjo.schools.app.fragments;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.optimalsolutionjo.schools.app.activities.R;

public class StudentProfileFragment extends Fragment {
    private View generalView;
    private Context context;
    private ImageView studentPic;
    private String selectedImagePath;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        generalView = inflater.inflate(R.layout.student_profile, container,
                false);
        context = generalView.getContext();
        studentPic = (ImageView) generalView.findViewById(R.id.studentPic);
        studentPic.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(
                        Intent.createChooser(intent, "Select Picture"), 1);
            }
        });
        return generalView;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == 1) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                studentPic.setImageURI(selectedImageUri);
            }
        }
    }

    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        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);
    }
}

我真的尝试过以各种可能的方式将onActivityResult放在任何地方 提前致谢

0 个答案:

没有答案