如何在Android中访问从onActivityResult方法到AsyncTask doInBackground方法的图像路径?

时间:2014-11-19 23:25:09

标签: java android android-asynctask

我在onActivityResult方法中从图库中选择图像,我可以在此方法中获取图像路径。现在我想在servlet和上传任务中上传这个图像,我想在AsyncTask doInBackground方法中做,但我无法访问从onActivityResult到doInBackGround方法的图像路径。

我不知道如何实现这一目标。我把我的代码放在这里:

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

}

1 个答案:

答案 0 :(得分:0)

那是因为你使路径成为onActivityResult()的局部变量。使其成为您活动的变量。更改字符串路径= getRealPathFromURI(..到path = getRealPathFromURI(...并在活动中声明String path =“”;。