Android Camera Intent无法访问onActivityResult

时间:2013-06-28 19:03:44

标签: java android android-intent

我正在尝试在我的应用程序中创建一个活动,其中包括两个按钮,“拍照”,调用意图使用默认相机拍摄照片,“选择现有”,这意味着选择一个现有的来自画廊的图像。正如许多教程中所述,人们需要使用onActivityResult函数。我的应用程序无法达到该功能(拍照后卡在相机上,按下勾号不会继续)。

但是,当此活动本身构建为单独的应用程序时,它会正确运行所有内容。只有当我将它导入另一个应用程序时,它才能达到onActivityResult。

这是我的代码:

public class PickPhotoActivity extends Activity {
Button btn1;
public String VALUE_NAME;
private boolean picAvailable = false;
private Uri uriSavedImage;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("UserLog","Entered PickPhotoActivity");
    setContentView(R.layout.activity_pick_photo);
    Log.d("UserLog","Inflated view");
    Intent intent = getIntent();
    VALUE_NAME = intent.getStringExtra("name");
    Log.d("Userlog", "Got intent, data " + VALUE_NAME);
    btn1 = (Button) findViewById(R.id.btn_takePhoto);
    btn1.setOnClickListener(new OnClickListener() {
           @Override
            public void onClick(View v) {
               Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
               File imagesFolder = new File(Environment.getExternalStorageDirectory(), "AppImages");
               imagesFolder.mkdirs();
               String filename = VALUE_NAME + ".jpg";
               File image = new File(imagesFolder, filename);
               uriSavedImage = Uri.fromFile(image);
               Log.d("UserLog", "Photo saved as: " + uriSavedImage);
               imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
               startActivityForResult(imageIntent,100);
          }
        });
    Button buttonLoadImage = (Button) findViewById(R.id.btn_chooseExisting);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 2);
        }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("UserLog","Result code" + Integer.toString(requestCode));
    if(requestCode==100){
        Log.d("UserLog","Getting Image By:Take New");
        ImageView imageView = (ImageView) findViewById(R.id.picture);
        imageView.setImageURI(uriSavedImage);
        picAvailable=true;
    }else if(requestCode==2){
        Log.d("UserLog","Getting Image By:Choose Existing");
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        ImageView imageView = (ImageView) findViewById(R.id.picture);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        picAvailable=true;
    }
}

}

我将Log.d放在代码中的不同位置。输出(由日志组成)是

Entered PickPhotoActivity
Inflated view
Got intent, data
Photo saved as: file///mnt/sdcard/AppImages/.jpg

停在这里。不输出结果码。 在旁注中,意图额外数据始终为空。 如果这有帮助,则使用来自另一个活动的startActivityForResult调用此活动本身。

0 个答案:

没有答案