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 が返されます。