下面是我的代码,任何人都可以帮助我,当我被要求从图库或相机中选择图像时,我可以在哪里放置裁剪图像代码。提前谢谢。
公共类MainActivity扩展了AppCompatActivity实现 View.OnClickListener {
//public static final String UPLOAD_URL = "http://192.168.0.137/image_json /upload.php";
public static final String URL_ADD = "http://192.168.0.104/image_json/addEmp.php";
public static final String URL_GET_ALL = "http://192.168.0.137/image_json/getAllEmp.php";
public static final String URL_GET_EMP = "http://192.168.0.137/image_json/getEmp.php?id=";
public static final String URL_UPDATE_EMP = "http://192.168.0.137/image_json/updateEmp.php";
public static final String URL_DELETE_EMP = "http://192.168.0.137/image_json/deleteEmp.php?id=";
// Keys that will be used to send the request to php scripts
public static final String KEY_EMP_ID = "id";
public static final String KEY_EMP_NAME = "name";
public static final String KEY_EMP_DESG = "desg";
public static final String KEY_EMP_SAL = "salary";
// JSON Tags
public static final String TAG_JSON_ARRAY = "result";
public static final String TAG_ID = "id";
public static final String TAG_NAME = "name";
public static final String TAG_DESG = "desg";
public static final String TAG_SAL = "salary";
// employee id to pass with intent
public static final String EMP_ID = "emp_id";
public static final String UPLOAD_KEY = "image";
private int PICK_IMAGE_REQUEST = 1;
int REQUEST_CAMERA = 0;
private Button buttonChoose;
private Button buttonUpload;
// private Button buttonView;
private EditText editTextName;
private EditText editTextDesg;
private EditText editTextSal;
private Button buttonAdd;
private Button buttonView;
private ImageView imageView;
private Bitmap bitmap;
private Uri filePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextDesg = (EditText) findViewById(R.id.editTextDesg);
editTextSal = (EditText) findViewById(R.id.editTextSalary);
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonView = (Button) findViewById(R.id.buttonView);
// Setting listeners to button
buttonAdd.setOnClickListener(this);
buttonView.setOnClickListener(this);
buttonChoose = (Button) findViewById(R.id.buttonChoose);
buttonUpload = (Button) findViewById(R.id.buttonUpload);
// buttonView = (Button) findViewById(R.id.buttonViewImage);
imageView = (ImageView) findViewById(R.id.imageView);
buttonChoose.setOnClickListener(this);
buttonUpload.setOnClickListener(this);
// buttonView.setOnClickListener(this);
}
private void addEmployee() {
final String name = editTextName.getText().toString().trim();
final String desg = editTextDesg.getText().toString().trim();
final String sal = editTextSal.getText().toString().trim();
class AddEmployee extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this, "Adding...",
"Wait...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Void... v)
{
String uploadImage = getStringImage(bitmap);
HashMap<String, String> params = new HashMap<String, String>();
params.put(KEY_EMP_NAME, name);
params.put(KEY_EMP_DESG, desg);
params.put(KEY_EMP_SAL, sal);
params.put(UPLOAD_KEY, uploadImage);
RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(URL_ADD, params);
Log.i("result",""+res);
return res;
}
}
AddEmployee ae = new AddEmployee();
ae.execute();
}
@SuppressLint("NewApi")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(
getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
/*
* Bundle extras = data.getExtras(); Bitmap photo = (Bitmap)
* extras.get("data");
*/
bitmap = getImageFileFromSDCard("abc.jpg");
imageView.setImageBitmap(bitmap);
} else {
Toast.makeText(getApplicationContext(), "bundal is null",
Toast.LENGTH_LONG).show();
}
}
private Bitmap getImageFileFromSDCard(String filename) {
Bitmap bitmap = null;
File outputFile = Environment.getExternalStorageDirectory();
File file = new File(outputFile, filename);
Log.i("pathstore", "" + file);
try {
FileInputStream fis = new FileInputStream(file);
bitmap = BitmapFactory.decodeStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bitmap;
}
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
@Override
public void onClick(View v) {
if (v == buttonChoose) {
selectImage();
// showFileChooser();
}
if (v == buttonAdd) {
addEmployee();
}
}
private void viewImage() {
// startActivity(new Intent(this, ImageListView.class));
}
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
String userChoosenTask = "Take Photo";
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
String userChoosenTask = "Choose from Library";
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void cameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File out = Environment.getExternalStorageDirectory();
out = new File(out, "abc.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(intent, REQUEST_CAMERA);
}
private void galleryIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"),
PICK_IMAGE_REQUEST);
}
}
答案 0 :(得分:0)
您可以在onActivityResult方法中裁剪部分,就像从图库或相机中选择照片后调用它一样。
我希望这个答案可以帮到你。
答案 1 :(得分:-1)
像这样使用你的意图。
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CROP_PIC_REQUEST_CODE);
祝你好运!