无法在Android应用程序中获得意图附加功能

时间:2011-07-22 13:12:10

标签: android android-activity android-intent extras

我正在尝试将额外内容(字符串)放入意图中。我使用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();
            }

        }
    }
}

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

谢谢!

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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字符串做什么?

答案 2 :(得分: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);  
        }  
    }  
}