当我尝试上传图片grom gallery时,我在logcat中收到此错误。当我直接从相机上传图像时它工作正常。以下是我的代码
public class OfflineMerchantRegisterActivity extends AppCompatActivity {
EditText edtUsername, edtPassword, edtConfirmPassword, edtEmail, edtMobile, edtServiceType, edtName, edtAddress, edtCity;
Button btnRegister, btnSelectImage, btnSelectCity, btnSelectType;
ImageView imgMerchant;
String offline_service_type;
LinearLayout layoutCity, layoutServiceType;
EditText edtService;
String service;
String username, password, confirmPassword, email, mobile, serviceType, name, address, city;
Context context;
private String userChoosenTask;
private static final int CAMERA_REQUEST = 1888;
private static final int GALLERY_REQUEST = 2;
Bitmap imageBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_offline_merchant_register);
context=OfflineMerchantRegisterActivity.this;
edtUsername = (EditText) findViewById(R.id.edtUsername);
edtPassword = (EditText) findViewById(R.id.edtPassword);
edtConfirmPassword = (EditText) findViewById(R.id.edtConfirmPassword);
edtEmail = (EditText) findViewById(R.id.edtEmail);
edtMobile = (EditText) findViewById(R.id.edtMobile);
edtServiceType = (EditText) findViewById(R.id.edtServiceType);
edtName = (EditText) findViewById(R.id.edtName);
edtAddress = (EditText) findViewById(R.id.edtAddress);
edtCity = (EditText) findViewById(R.id.edtCity);
edtService = (EditText) findViewById(R.id.edtService);
edtService.setText("Offline Merchant");
btnRegister = (Button) findViewById(R.id.btnRegister);
btnSelectImage = (Button) findViewById(R.id.btnSelectImage);
btnSelectCity = (Button) findViewById(R.id.btnSelectCity);
btnSelectType = (Button) findViewById(R.id.btnSelectType);
imgMerchant = (ImageView) findViewById(R.id.imgMerchant);
layoutCity = (LinearLayout) findViewById(R.id.layoutCity);
layoutServiceType = (LinearLayout) findViewById(R.id.layoutServiceType);
btnSelectCity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(OfflineMerchantRegisterActivity.this, MerchantCityActivity.class);
startActivityForResult(intent, 3);
}
});
btnSelectType.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(OfflineMerchantRegisterActivity.this, OfflineServiceTypeListActivity.class);
startActivityForResult(intent, 1);
}
});
layoutServiceType.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
layoutCity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isValid()){
//registerOfflineMerchant();
Intent intent = new Intent(OfflineMerchantRegisterActivity.this, OfflineMerchantConfirmActivity.class);
intent.putExtra("username", edtUsername.getText().toString());
intent.putExtra("password", edtPassword.getText().toString());
intent.putExtra("email", edtEmail.getText().toString());
intent.putExtra("mobile", edtMobile.getText().toString());
intent.putExtra("serviceType", edtServiceType.getText().toString());
intent.putExtra("name", edtName.getText().toString());
intent.putExtra("address", edtAddress.getText().toString());
intent.putExtra("city", edtCity.getText().toString());
if (imageBitmap!=null){
intent.putExtra("imageString", getStringImage(imageBitmap));
intent.putExtra("fileName", username+".png");
}
else {
//do nothing
}
intent.putExtra("service", edtService.getText().toString());
startActivity(intent);
}
}
});
btnSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);*/
selectImage();
}
});
}
public boolean isValidMail(String email) {
// TODO Auto-generated method stub
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
public boolean isValid(){
username = edtUsername.getText().toString().trim();
name = edtName.getText().toString().trim();
password = edtPassword.getText().toString().trim();
confirmPassword = edtConfirmPassword.getText().toString().trim();
email = edtEmail.getText().toString().trim();
mobile = edtMobile.getText().toString().trim();
serviceType = edtServiceType.getText().toString().trim();
address = edtAddress.getText().toString().trim();
city = edtCity.getText().toString().trim();
service = edtService.getText().toString().trim();
if (username.length() <=0){
Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Username", Toast.LENGTH_SHORT).show();
return false;
}
if (name.length() <=0){
Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Name", Toast.LENGTH_SHORT).show();
return false;
}
if (password.length() <=0){
Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Password", Toast.LENGTH_SHORT).show();
return false;
}
if (confirmPassword.length() <=0){
Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Password Again", Toast.LENGTH_SHORT).show();
return false;
}
if (!(password.equals(confirmPassword))) {
Toast.makeText(this, "Passwords Mismatch", Toast.LENGTH_SHORT).show();
return false;
}
if (email.length() <=0){
Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Email", Toast.LENGTH_SHORT).show();
return false;
}
if (!isValidMail(email)) {
Toast toast = Toast.makeText(this, "Please Enter Valid Email", Toast.LENGTH_SHORT);
toast.show();
return false;
}
if (mobile.length() <=9){
Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Mobile Number", Toast.LENGTH_SHORT).show();
return false;
}
if (serviceType.length() <=0){
Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Select Service Type", Toast.LENGTH_SHORT).show();
return false;
}
if (address.length() <=0){
Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter Address", Toast.LENGTH_SHORT).show();
return false;
}
if (city.length() <=0){
Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Select City", Toast.LENGTH_SHORT).show();
return false;
}
if (service.length() <= 0){
Toast.makeText(OfflineMerchantRegisterActivity.this, "Please Enter As Offline Service", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1) {
offline_service_type = data.getStringExtra("offlineServiceType");
edtServiceType.setText(offline_service_type);
}
if (resultCode == Activity.RESULT_OK){
if (requestCode == 3){
city = data.getStringExtra("city");
edtCity.setText(city);
}
}
if (resultCode == RESULT_OK && null != data) {
if (requestCode == GALLERY_REQUEST) {
Uri URI = data.getData();
String[] FILE = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(URI, FILE, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(FILE[0]);
String imageDecode = cursor.getString(columnIndex);
cursor.close();
imageBitmap = BitmapFactory.decodeFile(imageDecode);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
imgMerchant.setImageBitmap(imageBitmap);
//uploadImageToServer(getStringImage(bmp), username);
} else if (requestCode == CAMERA_REQUEST) {
imageBitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
imgMerchant.setImageBitmap(imageBitmap);
//uploadImageToServer(getStringImage(thumbnail), username);
}
}
}
}
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
byte[] imageBytes = outputStream.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (userChoosenTask.equals("Take Photo"))
cameraIntent();
else if (userChoosenTask.equals("Choose from Library"))
galleryIntent();
} else {
//code for deny
}
break;
}
}
private void selectImage() {
final CharSequence[] items = {"Take Photo", "Choose from Library",
"Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Add Photo");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result = Utility.checkPermission(context);
if (items[item].equals("Take Photo")) {
userChoosenTask = "Take Photo";
if (result)
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
userChoosenTask = "Choose from Library";
if (result)
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void galleryIntent() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY_REQUEST);
//startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
private void cameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}
public void onBackPressed() {
OfflineMerchantRegisterActivity.this.finish();
super.onBackPressed();
}
}
如何解决此错误?从图库中选择图像后,应用程序崩溃错误。
答案 0 :(得分:2)
当您填写意图的数据超过系统能够通过Intent包传输的数据时,通常会发生此错误。检查您在捆绑中放置的所有内容的大小。并且还要检查您从图库中选择的图像是否不是很大,因为系统可能无法将图像从图库传输到您的应用程序。相机代码正常工作,因为相机应用程序通过Intent传输低分辨率图像。