自动调用ACTION_IMAGE_CAPTURE后调用onDestroy

时间:2017-03-02 03:32:05

标签: android

public abstract class Write_MyArticle extends AppCompatActivity{


public Button btn_plus, btn_post, btn_delete;
public EditText edit_title, edit_contents;
public Uri imageUri;
public String absolutePath =null;
public ImageView image;
public RelativeLayout layout;
public Context mContext;
public Activity activity;
public Bitmap imageBitmap;

public final int PICK_FROM_CAMERA = 900;


public void onDestroy(){
    super.onDestroy();
    Log.d("message", " passing onDestroy");
    if(imageBitmap != null && imageUri != null){
        Log.d("message", "delete picture in gallery ");
        getContentResolver().delete(imageUri, null,null);
    }
    imageBitmap = null;
    imageUri = null;
}

protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    init();
    mContext = this;
    activity = this;
  //  call_camera();
}

public void call_camera(){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, PICK_FROM_CAMERA);
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);


    Log.d("message", "took picture ");

    if(resultCode != RESULT_OK)
        return;

    if(requestCode == PICK_FROM_CAMERA){
        //선택한 사진의 uri
        imageUri = data.getData();

        Log.d("message", "URI : "+imageUri);
        // 선택한 사진의 절대경로 구하는 것
        Cursor c = this.getContentResolver().query(imageUri, null, null, null, null);
        c.moveToNext();
        absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));


        Log.d("message", "absolute path of the picture : "+absolutePath);

        imageBitmap = CameraUtil.resizeBitmap(absolutePath);
        imageBitmap = CameraUtil.fixOrientation(imageBitmap, absolutePath);

        Log.d("메시지", "path : "+imageUri.getPath());
        Glide.with(this).load(imageUri).into(image);
        btn_plus.setVisibility(View.GONE);
        layout.setVisibility(View.VISIBLE);
    }
}

public abstract void init();

}

下面是一个扩展上面的类的活动

public class Activity_MyDiary extends Write_MyArticle implements View.OnClickListener{

private final int DIARY_TYPE = 1;
private String  title, comments;



public void init(){
    setContentView(R.layout.activity_feed_mydiary);
    activity = this;
    mContext = this;
    btn_plus = (Button)findViewById(R.id.btn_feed_myDiary_plus);
    btn_plus.setOnClickListener(this);
    btn_post = (Button)findViewById(R.id.btn_feed_myDiary_post);
    btn_post.setOnClickListener(this);
    btn_delete = (Button)findViewById(R.id.btn_feed_myDiary_delete);
    btn_delete.setOnClickListener(this);
    edit_title = (EditText)findViewById(R.id.edit_feed_myDiary_title);
    edit_contents = (EditText)findViewById(R.id.edit_feed_myDiary_contents);
    image = (ImageView)findViewById(R.id.image_feed_myDiary_captured);
    layout = (RelativeLayout)findViewById(R.id.layout_feed_myDiary_image);

}


public void onClick(View v){

    switch (v.getId()){

        case R.id.btn_feed_myDiary_plus:
            call_camera();
            break;

        case R.id.btn_feed_myDiary_post:


            if(absolutePath == null){
                Toast.makeText(mContext, "사진이 없습니다", Toast.LENGTH_SHORT).show();
                return;
            }
            if(edit_title.getText().toString().equals("")){
                Toast.makeText(mContext, "제목을 작성해주세요", Toast.LENGTH_SHORT).show();
                return;
            }
            if(edit_contents.getText().toString().equals("")){
                Toast.makeText(mContext, "내용을 입력해 주세요", Toast.LENGTH_SHORT).show();
                return;
            }

            title = edit_title.getText().toString();
            comments = edit_contents.getText().toString();

            DB_SendPicture db_sendPicture = new DB_SendPicture(this, this);
            db_sendPicture.setOnResultPicture(sendPictureListener);
            db_sendPicture.execute(imageBitmap);


            break;

        case R.id.btn_feed_myDiary_delete:
            if(imageUri == null){
                Toast.makeText(mContext, "사진이 없습니다", Toast.LENGTH_SHORT).show();
                return;
            }
            getContentResolver().delete(imageUri, null,null);
            call_camera();
            break;
    }


}

我的应用程序有一个调用相机并捕获图像的功能。 当我单击类编号2中的按钮时,它调用祖先类中的方法call_camera()(上一个) 如您所见,call_camera()方法使用ACTION_IMAGE_CAPTURE并将结果传递给onActivityResult()

我有2部手机来测试我的应用程序(galaxy S4和galaxy J3) 在Galaxy J3上一切正常。 但是......当我尝试用galaxy S4代码时, 保存图片后,手机在第1类写入onDestroy()。

我认为没有理由调用onDestroy(),因为活动仍在屏幕上。

此外,如果我在创建时调用call_camera()(而不是单击按钮) 它也适用于星系S4 ...

1 个答案:

答案 0 :(得分:0)

捕捉图像相机会导致方向改变,即活动被破坏的时间。

我在使用它时使用它时遇到的类似问题。 所以,为了停止我使用下面代码的方向。

在你的清单文件中添加这些文件。

<activity
    android:name=".CameraActivity"
    android:screenOrientation="portrait"
    android:configChanges="orientation|keyboardHidden|screenSize"
    >
</activity>

希望这些可以帮助你。