0

我正在尝试将附加内容(字符串)放入意图中。我使用 startActivityForResult 和 onActivityResult 在另一边获取我的附加功能。

但我不明白为什么它不起作用!这是我的代码:

    buttonCamera.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra("abc", "test");
            startActivityForResult(intent, PHOTO_RESULT);

        }
    }); 

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PHOTO_RESULT) {
        if (resultCode == RESULT_OK) {

            Bundle extras = data.getExtras();
            if (extras != null) {
                String abc = extras.getString("abc");
                Toast.makeText(getApplicationContext(), abc, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "can't get", Toast.LENGTH_SHORT).show();
            }

        }
    }
}

我总是得到一个空的吐司,所以额外的不是空的..但我不能得到字符串..

谢谢 !

4

3 回答 3

0

显然你会得到一个空的吐司。您为意图 new Intent(MediaStore.ACTION_IMAGE_CAPTURE) 增加了额外的内容,并且您在 onActivity 结果中获得的 Intent 数据不同,(从相机活动返回)。所以将值存储在类变量中可以正常工作。

于 2011-07-22T13:20:51.007 回答
0

编辑:要告诉相机文件名,你可以使用这个:

File mFile = new File(path, filename);
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
activity.startActivityForResult(intent, TAKE_PHOTO_CODE);

相机活动将获取您的额外意图数据,但不会返回“abc”字符串。您不能告诉相机活动返回“abc”以获得结果。你想用那个 abc 字符串做什么?

于 2011-07-22T13:58:16.313 回答
0
package com.sachin.cameracapture;  

import android.app.Activity;  
import android.content.Intent;  
import android.graphics.Bitmap;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.ImageView;  

public class MainActivity extends Activity {  
    /** Called when the activity is first created. */  

    Button btnTakePhoto;  
    ImageView imgTakenPhoto;  
    private static final int CAM_REQUREST = 1313;  

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  

        btnTakePhoto = (Button) findViewById(R.id.button1);  
        imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);  

        btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());  
    }  

    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        // TODO Auto-generated method stub  
        super.onActivityResult(requestCode, resultCode, data);  

          if (requestCode == CAMERA_PIC_REQUEST) {  
              Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  
              imgTakenPhoto.setImageBitmap(thumbnail);  
          }  
    }  

    class btnTakePhotoClicker implements Button.OnClickListener  
    {  
        @Override  
        public void onClick(View v) {  
            // TODO Auto-generated method stub  

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);  
        }  
    }  
}  
于 2012-05-16T15:09:28.530 回答