【问题标题】:Android App turn off without error when using camera使用相机时Android App关闭且没有错误
【发布时间】:2015-04-29 14:15:59
【问题描述】:

有个人头像,可以从照片中选择和拍照。以前可以正常使用,但是今天突然尝试上传照片和拍照,应用程序关闭没有错误但显示:

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

有人可以帮助我吗?非常感谢!

【问题讨论】:

  • 最好显示代码而不是链接。也许你错过了一些东西。您的logcat 上也应该有一些东西,请尝试重新启动ADB
  • @hrskrs 感谢您的建议。我添加了我的代码,并在 logcat 中显示了内容。我已经重新启动亚行,但问题仍然存在。

标签: android android-camera


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2018-12-28
    • 2018-03-31
    • 2012-03-29
    相关资源
    最近更新 更多