Newbee Newbee - 2 months ago 8
Android Question

How to set data in Activity and get in java class using interface

Updated :



I have build a image cropping app its running fine, but now I want to save cropped image name as textbox value.

In short I am trying to set textbox value in object and get object value in java Class. I have tried several techniques, recently I am trying to get,set data by using interface technique and the image is saved as ".jpg"only.

I would love to know where am I going wronk?

Following is the code I have tried.

MainActivity



public class TestActivity extends AppCompatActivity implements CropHandler, View.OnClickListener {

public static final String TAG = "TestActivity";

ImageView mImageView;
EditText formnumber;

String formid;

CropParams mCropParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
mCropParams = new CropParams(this);
mImageView = (ImageView) findViewById(R.id.image);
formnumber =(EditText)findViewById(R.id.FormNumber);


findViewById(R.id.bt_crop_capture).setOnClickListener(this);
findViewById(R.id.bt_crop_gallery).setOnClickListener(this);


}

@Override

public void onClick(View v) {
mCropParams.refreshUri();
formid=formnumber.getText().toString();

// Intent i = new Intent(TestActivity.this, CropHelper.class);
// i.putExtra("Id",formid);

if(formid.matches(""))
{
Toast.makeText(getApplicationContext(),"Please Enter Application Id",Toast.LENGTH_SHORT).show();
}
else
{
switch (v.getId()) {
case R.id.bt_crop_capture: {
mCropParams.enable = true;
mCropParams.compress = false;

Intent intent = CropHelper.buildCameraIntent(mCropParams);

startActivityForResult(intent, CropHelper.REQUEST_CAMERA);

}
break;
case R.id.bt_crop_gallery: {
mCropParams.enable = true;
mCropParams.compress = false;
Intent intent = CropHelper.buildGalleryIntent(mCropParams);
startActivityForResult(intent, CropHelper.REQUEST_CROP);
}
break;

}
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
CropHelper.handleResult(this, requestCode, resultCode, data);
if (requestCode == 1) {
Log.e(TAG, "");
}
}

@Override
public void onTaskComplete(String response) {
onTaskComplete(this.formid);
}
}


CropHelper Class



public class CropHelper {
public static final String TAG = "CropHelper";
/**
* request code of Activities or Fragments
* You will have to change the values of the request codes below if they conflict with your own.
*/
public static final int REQUEST_CROP = 127;
public static final int REQUEST_CAMERA = 128;
public static final int REQUEST_PICK = 129;

public static String AppId;

public static final String CROP_CACHE_FOLDER = "PhotoCropper";

public static Uri generateUri() {
File cacheFolder = new File(Environment.getExternalStorageDirectory() + File.separator + CROP_CACHE_FOLDER);
if (!cacheFolder.exists()) {
try {
boolean result = cacheFolder.mkdir();

Log.d(TAG, "generateUri " + cacheFolder + " result: " + (result ? "succeeded" : "failed"));
} catch (Exception e) {
Log.e(TAG, "generateUri failed: " + cacheFolder, e);
}
}

// String name = String.format("image-%d.jpg", System.currentTimeMillis());
String name = String.format(AppId.toString()+".jpg",System.currentTimeMillis());
return Uri
.fromFile(cacheFolder)
.buildUpon()
.appendPath(name)
.build();
}

@Override
public void onTaskComplete(String response) {
AppId=response;
}
}


Interface



public interface CropHandler
{

void onPhotoCropped(Uri uri);

void onCompressed(Uri uri);

void onTaskComplete(String response);

void onCancel();

void onFailed(String message);

void handleIntent(Intent intent, int requestCode);

CropParams getCropParams();
}

Answer

You dont need to use Interface for that. Just set formid to EditText value and define formid as static and get the return value in your CropHelper class. followings are the changes you need to do in your code.

Activity

public static String formid=null;
// set formid value
    formid=formnumber.getText().toString();

Class

Here you need to create an object of your Activity and get formid value in genrateuri function.

        public static Uri generateUri() {
        MainActivity my_objec= new MainActivity();
        String id= my_objec.formid;
        File cacheFolder = new File(Environment.getExternalStorageDirectory() + File.separator + CROP_CACHE_FOLDER);
        if (!cacheFolder.exists()) {
            try {
                boolean result = cacheFolder.mkdir();

                Log.d(TAG, "generateUri " + cacheFolder + " result: " + (result ? "succeeded" : "failed"));
            } catch (Exception e) {
                Log.e(TAG, "generateUri failed: " + cacheFolder, e);
            }
        }

//        String name = String.format("image-%d.jpg", System.currentTimeMillis());
        String name = String.format(""+id+".jpg",System.currentTimeMillis());
        return Uri
                .fromFile(cacheFolder)
                .buildUpon()
                .appendPath(name)
                .build();
    }