user1735856
user1735856

Reputation: 285

Get value from protected void class

i´m going my first steps in java: I have the following problem. From my protected class 'onActivityResult' I need to have the String in my class 'ImageUploadTask'. How can I do this?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case PICK_IMAGE:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImageUri = data.getData();
            String filePath = null;

            try {
                ...

                } else if (filemanagerstring != null) {
                    filePath = filemanagerstring;
                } 
                if (filePath != null) {
                    decodeFile(filePath);
                    // get fileName //
                    String fileName = new File(filePath).getName();
                    Log.e(TAG, "Filename:" + fileName);


                } 
        break;
    default:
    }
}

class ImageUploadTask extends AsyncTask<Void, Void, String> {
    private static final String TAG = "GalleryPreviewView";

    @Override
    protected String doInBackground(Void... unsued) {
        try {
            ....
                     Log.e(TAG, "Filename:" + fileName);
        }

    }

    @Override
    protected void onProgressUpdate(Void... unsued) {

    }

}

Upvotes: 0

Views: 912

Answers (2)

Simon Dorociak
Simon Dorociak

Reputation: 33495

i´m going my first steps in java: I have the following problem. From my protected class 'onActivityResult' I need to have the String in my class 'ImageUploadTask'. How can I do this?

Probably you want to pass fileName variable to your AsyncTask subclass. You have a few options:

You can declare "class-scope" variable and only initialise it in your onActivityResult method:

if (filePath != null) {
   decodeFile(filePath);
   fileName = new File(filePath).getName();
   Log.e(TAG, "Filename:" + fileName);
}

and then pass variable to AsyncTask class like this:

class ImageUploadTask extends AsyncTask<String, Void, String> {
    private static final String TAG = "GalleryPreviewView";

    @Override
    protected String doInBackground(String... params) {
       String fileName = params[0];
    }

An usage:

ImageUploadTask task = new ImageUploadTask();
task.execute(fileName);

But since your ImageUploadTask is probably inner class of your Activity class, you have direct access to all variables declared in "parent" class.

Upvotes: 1

Mike Marshall
Mike Marshall

Reputation: 7850

If you are using ImageUploadTask as an embedded class of your activity class then you can do this:

MyActivityClass.this.activityfunction();

or 

MyActivityClass.this.activityFieldVariable;

Upvotes: 0

Related Questions