Issue
I am selecting an image from gallery in onActivityResult method and I can get image path in this method. Now I want to upload this image in servlet and this uploading task, I want to do in AsyncTask doInBackground method, but I can not access image path from onActivityResult to doInBackGround method.
I don't know how to achieve this. I am putting my code here:
public class Photos extends ActionBarActivity {
private static final int REQUEST_ID = 1;
private static final int HALF = 6;
Button mUpload;
ImageView mPhoto;
Button mDoUpload;
Intent data;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photos);
mPhoto = (ImageView) findViewById(R.id.uploaded_photo);
mUpload = (Button) findViewById(R.id.btn_photoshare);
mDoUpload = (Button) findViewById(R.id.btn_upload);
mUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_ID);
}
});
mDoUpload.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
InputStream stream = null;
if(requestCode == REQUEST_ID && resultCode == Activity.RESULT_OK)
{
try
{
stream = getContentResolver().openInputStream(data.getData());
//System.out.println(data.getData());
Bitmap original = BitmapFactory.decodeStream(stream);
((ImageView)findViewById(R.id.uploaded_photo)).setImageBitmap(Bitmap.createScaledBitmap(original, original.getWidth()/HALF,
original.getHeight()/HALF, true));
String path = getRealPathFromURI(getApplicationContext(), data.getData());
System.out.println(path);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(stream != null)
{
try
{
stream.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
public String getRealPathFromURI(Context context,Uri contentUri)
{
Cursor cursor = null;
try
{
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
// System.out.println(cursor.getString(column_index));
return cursor.getString(column_index);
}
finally {
if (cursor != null) {
cursor.close();
}
}
}
private class sendFile extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
HttpURLConnection httpConn=null;
try
{
File uploadFile = new File(path);
//here I dont know how to access selected image path
}
catch(Exception ex)
{
ex.printStackTrace();
}
return null;
}
}
Solution
That's because you made path a local variable of onActivityResult(). Make it a variable of your activity. Change String path = getRealPathFromURI(.. to path = getRealPathFromURI(... And declare String path=""; in the activity.
Answered By - greenapps
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.