2016-07-15 309 views
0

您好我正在製作Android應用程序,我做了一個表單,成功地將所有數據添加到MySQL服務器。我再添加一個按鈕來上傳圖像並在onActivityResult上成功獲取該圖像。但是,當我是外地人onActivityResult返回試圖調用的ImagePath(字符串)空always.I認爲其片段字符串返回空片段onActivityResult

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; 
} 

} 

每當我叫onActivityResult以外的ImagePath的曲子發送圖像到它總是返回NULL服務器。當我試圖做同樣的事情,在活動的節目onActivityResult.Please外樹形象告訴我的路在哪裏做錯了

+0

當您的位圖爲空時,您不必詢問圖像上傳。你最好問問你如何獲得一個位圖。所以你可以先刪除幾乎所有的代碼,然後只讓相關的代碼在這裏。專注於你的問題! – greenapps

+0

@greenapps我根據你細化問題,但圖像路徑返回null我試着在它的工作相同的代碼,但沒有在片段 –

+0

'每當我打電話ImagePath'。您不能調用ImagePath,因爲它不是函數。您只能分配一個值或確定一個值。現在你在哪裏使用OnActivityResult? – greenapps

回答

0

首先讓

String ImagePath=null; 

String ImagePath; 

然後從你onClick像這樣從imagePath獲取位圖

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(); 

} 

然後將其作爲Strin處理g Sending_Data_To_Server()與其他人一樣AsyncTask字符串

+0

仍然爲空,我認爲它是因爲fragment.Thank你爲你回答我將編輯我的問題一次 –

+0

如果它仍然爲空,那麼需要挖掘代碼可能是你的權利其片段 – androidXP

+0

首先make String ImagePath = null; 分成 String ImagePath;'。爲什麼?分配一個空值更好,我認爲。 – greenapps