0

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 because 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 it shows the path of image outside onActivityResult.Please tell me where I am doing wrong

user3678528
  • 1,741
  • 2
  • 18
  • 24
Kabir Raz
  • 23
  • 6
  • You do not have to ask about image uploading when your bitmap is null. You better would ask how you could get a bitmap. So you can remove nearly all code first and only let relevant code here. Concentrate on your problem! – greenapps Jul 15 '16 at 10:52
  • @greenapps i refine question according to you but the image path returns null i tried same code in activity its working there but not in fragment – Kabir Raz Jul 19 '16 at 05:45
  • `Whenever i call ImagePath`. You cannot call ImagePath as it is not a function. You only can assign it a value or determine a value. Now where are you using it outside OnActivityResult? – greenapps Jul 19 '16 at 05:53
  • @greenapps i m calling ImagePath string in Log not as function to check whether i am getting any value in it i tried to convert ImagePath into bitmap but its won't until ImagePath has some value i calling it in Onclick Submit – Kabir Raz Jul 19 '16 at 06:05
  • `ImagePath=getPath(imageNameURL);`. If i see such a statement i think that ImagePath is a String. And getPath() returns a String. But ... `public Bitmap getPath(Uri uri){` returns a Bitmap. So whats happening? This will not even compile. Or do you have an overloaded function? – greenapps Jul 19 '16 at 06:05
  • `i m calling ImagePath string` ?? I just tried to explain you that you cannot call a string. – greenapps Jul 19 '16 at 06:11
  • @greenapps I tried same code in Activity and its works there fine but not in fragment. sorry for the confusion its public String getPath(Uri uri).Thank you – Kabir Raz Jul 19 '16 at 06:15
  • `ImagePath=getPath(imageNameURL); bitmapImage=getPath(data.getData()); Log.d("REGI-ONACTIVITYRESULT",bitmapImage.toString());`. What a mess. Your Log is pointless. You should Log ImagePath to see if it really gets a value in onActivityResult. – greenapps Jul 19 '16 at 06:30
  • @greenapps i updated code ImagePath is printing location of image perfectly on logcat but not in Onclick – Kabir Raz Jul 19 '16 at 06:45
  • `ImagePath=getPath(imageNameURL); bitmapImage=getPath(data.getData());`. Is that really your code? That will not even compile if you only have one getPath() function. Still a mess. It becomes more and more difficult to believe you. – greenapps Jul 19 '16 at 06:59
  • @greenapps 'code' bitmapImage=getPath(data.getData()); was in comment and if you look below you will find getPath() function.Please sort out my problem its really headache for me – Kabir Raz Jul 19 '16 at 08:28

1 Answers1

0

First of all make

String ImagePath=null; 

into

String ImagePath;

then from you onClick get Bitmap from imagePath like this

 if (ImagePath!=null){
 Log.d("ONCLICIK---------------",ImagePath);
 Bitmap bitmapFromPath= BitmapFactory.decodeFile(ImagePath);

 String baseImage=Convert_To_Base64(bitmapFromPath);
 new Sending_Data_To_Server().execute(First,Last,Phone,Mail,Pass,lattitude,longitude,accuracy,provider,baseImage);
                    startActivity(AllData);
                    getActivity().finish();

 }

then handle it as String in your Sending_Data_To_Server() AsyncTask like others String

androidXP
  • 1,692
  • 3
  • 27
  • 58
  • still null i think its because of fragment.Thank you for you answer i will edit my question one more time – Kabir Raz Jul 18 '16 at 17:54
  • If its still null then need to dig into code may be you right its cuz of fragment – androidXP Jul 19 '16 at 04:58
  • `First of all make String ImagePath=null; into String ImagePath;`. Why? Assigning a null is better i think. – greenapps Jul 19 '16 at 05:54
  • @greenapps If an Object reference has been declared but not instantiated, its value is null. http://stackoverflow.com/questions/16699593/uninitialized-object-vs-object-initialized-to-null – androidXP Jul 19 '16 at 06:21
  • Ok. Thanks. But no reason to say 'First of all make'. Whats the problem doing so? – greenapps Jul 19 '16 at 06:32
  • @greenapps reason for change it cuz its reference is pointing to null, null is like any reference, it takes a space of a native pointer, that is 4 byte on 32-bit machines and 8 bytes on 64-bit machines.Its just code clean up nothing else – androidXP Jul 19 '16 at 09:35