android如何在中设置数据 Activity 和 java 类中得到

发布时间: 2016/10/10 20:02:56
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

更新︰

我有 build 保存裁剪的图像名称文本框的值作为其运行很好,但现在想裁剪应用程序图像。

总之,我要在对象中设置文本框的值并获取 java 类中的对象值。我已经尝试了几个技术,最近我想获得,通过使用接口技术设置数据和图像保存为".jpg"只。

要知道在那里我会 wronk 爱

以下是我用过的代码。

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 类

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

接口

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

解决方法 1:

你不需要使用接口的。只是设置 formid EditText 价值和作为静态定义 formid 和获取的返回值在你 CropHelper 类别以下是你需要在你的代码中做的更改。

Activity

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

在这里,您需要创建的对象你 Activity 和 genrateuri 函数中获得 formid 的价值。

        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();
    }
官方微信
官方QQ群
31647020