1

我正在尝试在我的应用程序中创建一个活动,该活动由两个按钮“拍照”,它调用使用默认相机拍照的意图,“选择现有”,它调用从现有图像中选择现有图像的意图画廊。正如许多教程中所述,需要使用 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 调用的。

4

0 回答 0