您好我正在製作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外樹形象告訴我的路在哪裏做錯了
當您的位圖爲空時,您不必詢問圖像上傳。你最好問問你如何獲得一個位圖。所以你可以先刪除幾乎所有的代碼,然後只讓相關的代碼在這裏。專注於你的問題! – greenapps
@greenapps我根據你細化問題,但圖像路徑返回null我試着在它的工作相同的代碼,但沒有在片段 –
'每當我打電話ImagePath'。您不能調用ImagePath,因爲它不是函數。您只能分配一個值或確定一個值。現在你在哪裏使用OnActivityResult? – greenapps