String Returns Null outside onActivityResult in Fragment

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 6

Jul 15, 2016 12:20:41 AM via Website

Hi i am making android app and i made a form which add all data into mysql server successfully. I add one more button to upload image and getting that image successfully on onActivityResult. but when i am trying call ImagePath(String) outside onActivityResult it returns null always.I think its cuz of Fragment

public class Registration_Fragment extends Fragment implements View.OnClickListener {

EditText First_Name;
EditText Last_Name;
EditText Phone_Number;
EditText Email_Address;
EditText Password;
ImageView PreviewImage;
Button Submit;
Button Upload;

Uri imageNameURL;
Bitmap bitmap;
String ImagePath;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.reg_fragment,container,false);
First_Name= (EditText) view.findViewById(R.id.first_Name);
Last_Name= (EditText) view.findViewById(R.id.last_Name);
Phone_Number= (EditText) view.findViewById(R.id.phone);
Email_Address= (EditText) view.findViewById(R.id.mailID);
Password= (EditText) view.findViewById(R.id.password);
PreviewImage= (ImageView) view.findViewById(R.id.Image_Preview);
Submit= (Button) view.findViewById(R.id.Submit);
Upload= (Button) view.findViewById(R.id.Uploader);

Submit.setOnClickListener(this);
Upload.setOnClickListener(this);    

return view;

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.Submit:
Log.d("Registration Fragment","Submit Button Pressed");
Intent AllData=new Intent(getActivity(),DemoScreen3.class);

        //Now Collect Form Data 

        String First=First_Name.getText().toString();
        String Last=Last_Name.getText().toString();
        String Phone=Phone_Number.getText().toString();
        String Mail=Email_Address.getText().toString();
        String Pass=Password.getText().toString();


        if (ImagePath!=null){
            Log.d("REGI-ON CLICK==========",ImagePath);
        }
        else {
            Log.d("REGI-ON CLICK==========","Cannot Find ImagePath");// THIS LOG ALWAYS CALLED 
        }

//AYSYNC TASK CALLING FROM FRAGMENT

        new Sending_Data_To_Server().execute(First,Last,Phone,Mail,Pass);    

        Log.d("REGISTRATION CLASS","----ALL DATA READY AND CAPTURED");    
        startActivity(AllData);    
        break;

    case R.id.Uploader:
        pickImage();    
}

}

private void pickImage(){
Intent intent=new Intent();
Log.d("Registration Fragment","Calling PickImage");
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Log.d("Registration Fragment","Calling PickImage 2");
startActivityForResult(Intent.createChooser(intent,"Complete Action Using"),1);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==1 && resultCode==getActivity().RESULT_OK){

    imageNameURL=data.getData();
    ImagePath=getPath(imageNameURL);
    Log.d("onActivityResult",ImagePath);

    bitmap= BitmapFactory.decodeFile(ImagePath);       

    PreviewImage.setImageBitmap(bitmap);   
}   

}

public String getPath(Uri uri){
String[] projection={MediaStore.Images.Media.DATA};
Cursor cursor=getActivity().getContentResolver().query(uri,projection,null,null,null);
int Column_Index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
ImagePath=cursor.getString(Column_Index);
cursor.close();
//Bitmap bitmap= BitmapFactory.decodeFile(ImagePath);
return ImagePath;
}

public static String Convert_To_Base64(Bitmap bmp){
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] imageBytes=baos.toByteArray();
String encodedImage= Base64.encodeToString(imageBytes,Base64.DEFAULT);
return encodedImage;
}

}

Whenever i call ImagePath outside onActivityResult to send image to the server its always returns NULL. And when i tried to do same thing on Activity its shows the path of image outside onActivityResult.Please tell me where i am doing wrong

— modified on Jul 19, 2016 11:35:39 PM