我在将值传递给 onActivityResult() 方法时遇到问题。这是我的代码:
public class UserUploadActivity extends Activity{
String userID;
Uri currImageURI;
String path;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_upload);
Bundle gotBundle = getIntent().getExtras();
userID = gotBundle.getString("key");
Intent selectImageIntent = new Intent();
selectImageIntent.setType("image/*");
selectImageIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(selectImageIntent, "Select Picture"), 1);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == 1) {
// currImageURI is the global variable I’m using to hold the content:
currImageURI = data.getData();
path = getRealPathFromURI(currImageURI);
//covert to base64 image
Bitmap bitmapOrg = BitmapFactory.decodeFile(path);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
//pass the base64 code to string and send it to php file
String ba1=Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
nameValuePairs.add(new BasicNameValuePair("id",userID));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("website link");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}catch(Exception e){
Toast.makeText(getBaseContext(), "Hindi Gumana", Toast.LENGTH_LONG).show();
}
}
}
}// end method onActivityResult
public String getRealPathFromURI(Uri contentUri)
{
String [] proj={MediaStore.Images.Media.DATA};
android.database.Cursor cursor = managedQuery( contentUri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}//end method getRealPathFromURI
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_user_upload, menu);
return true;
}
}
但我唯一的问题是如何将 userID 的值传递给 onActivityResult。已经发布了我的整个代码。我的问题是关于意图。
我已经在 onCreate 方法中使用了 toast 来查看它的价值和工作正常。但是每当我尝试将其传递给 onActivityResult 时,它总是返回 null。