使用相机时,Android应用程序会关闭而不会出错

时间:2015-04-29 14:15:59

标签: android android-camera

有一张个人资料照片,可以从照片中选择并拍照。它之前完美运行,但突然之间,今天,我尝试上传照片并拍照,该应用关闭没有错误,但显示:

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);
}
有人能帮帮我吗?非常感谢你!

1 个答案:

答案 0 :(得分:2)

无论如何,我解决了这个问题。由于没有错误,所以代码和逻辑都是正确的。我只是在此活动下的AndroidManifest.xml文件中删除了android:noHistory =“true”。