Android 使用相机时应用程序无误关闭
Android App turn off without error when using camera
有个人头像可以选择照片拍照。之前用的好好的,今天突然想上传照片和拍照,app没有报错就关掉了但是显示:
D/I'M HERE 1﹕ After click the camera button1
D/I'M HERE 2﹕ After click the camera button
D/I'M HERE Take Photo﹕ After click the camera button
W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
这个改图代码我没有做任何改动。只需添加整个应用程序的其他功能即可。
我将此代码用于选择图片功能
http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample/.
这是我的代码:
// JSON Response node names
private static String create_success = "create_success";
int REQUEST_CAMERA = 0, SELECT_FILE = 1;
Button btnSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
/********* Change Profile Picture Event Begin **********/
btnChangeProfilePic = (Button)findViewById(R.id.changeProilePicBtn);
ProfilePicimageView = (ImageView) findViewById(R.id.profilepic);
btnChangeProfilePic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("I'M HERE 1", "After click the camera button1");
selectImage();
}//end onClick
});
/********* Change Profile Picture Event End **********/
}//end onCreate
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
Log.d("I'M HERE 2", "After click the camera button");
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.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")) {
Log.d("I'M HERE Take Photo", "After click the camera button");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Log.d("I'Choose from Library", "After click the camera button");
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("onActivityResult", "onActivityResult");
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
Log.d("onCaptureImageResult", "onCaptureImageResult");
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, 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();
}
ProfilePicimageView.setImageBitmap(thumbnail);
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Uri selectedImageUri = data.getData();
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(selectedImageUri, projection, null, null,
null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
Bitmap bm;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(selectedImagePath, options);
ProfilePicimageView.setImageBitmap(bm);
}
有人可以帮助我吗?非常感谢!
无论如何,我解决了这个问题。既然没有错误,那么代码和逻辑都是正确的。我只是删除了 activity.
下的 AndroidManifest.xml 文件中的 android:noHistory="true"
有个人头像可以选择照片拍照。之前用的好好的,今天突然想上传照片和拍照,app没有报错就关掉了但是显示:
D/I'M HERE 1﹕ After click the camera button1
D/I'M HERE 2﹕ After click the camera button
D/I'M HERE Take Photo﹕ After click the camera button
W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
这个改图代码我没有做任何改动。只需添加整个应用程序的其他功能即可。
我将此代码用于选择图片功能
http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample/.
这是我的代码:
// JSON Response node names
private static String create_success = "create_success";
int REQUEST_CAMERA = 0, SELECT_FILE = 1;
Button btnSelect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
/********* Change Profile Picture Event Begin **********/
btnChangeProfilePic = (Button)findViewById(R.id.changeProilePicBtn);
ProfilePicimageView = (ImageView) findViewById(R.id.profilepic);
btnChangeProfilePic.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("I'M HERE 1", "After click the camera button1");
selectImage();
}//end onClick
});
/********* Change Profile Picture Event End **********/
}//end onCreate
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
Log.d("I'M HERE 2", "After click the camera button");
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.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")) {
Log.d("I'M HERE Take Photo", "After click the camera button");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Log.d("I'Choose from Library", "After click the camera button");
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("onActivityResult", "onActivityResult");
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
Log.d("onCaptureImageResult", "onCaptureImageResult");
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, 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();
}
ProfilePicimageView.setImageBitmap(thumbnail);
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Uri selectedImageUri = data.getData();
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(selectedImageUri, projection, null, null,
null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
Bitmap bm;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(selectedImagePath, options);
ProfilePicimageView.setImageBitmap(bm);
}
有人可以帮助我吗?非常感谢!
无论如何,我解决了这个问题。既然没有错误,那么代码和逻辑都是正确的。我只是删除了 activity.
下的 AndroidManifest.xml 文件中的 android:noHistory="true"