如何启动相机并将照片发送到另一个活动

时间:2015-09-12 16:31:18

标签: android

我现在尝试了几天,但我似乎无法得到它。 这是我的代码,用于启动相机并发送到另一个活动。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show);
    Intent intent = getIntent();
    iv = (ImageView) findViewById(R.id.showImage);
    iv.setImageURI(Uri.parse(getIntent().getStringExtra("pictureUri")));
}

现在在接收活动中我有以下代码:

RequestDispatcher req = request.getRequestDispatcher("/bin/wcmcommand");
req.include(request,response);

请有人帮帮我,这是杀了我!

3 个答案:

答案 0 :(得分:2)

尝试这个,它的工作。

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private static final int CAMERA_REQUEST = 100;
    private static final int STORAGE_PERMISSION_CODE = 1;
    Button btnCapture;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnCapture = (Button) findViewById(R.id.btnCapture);

        btnCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

            Intent intent = new Intent(MainActivity.this, NextActivity.class);
            intent.putExtra("image", thumbnail);
            startActivity(intent);
        }
    }

}

NextActivity.java

ImageView imgView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageview);

        imgView = (ImageView) findViewById(R.id.imgView);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            Bitmap image = (Bitmap) extras.get("image");
            if (image != null) {
                imgView.setImageBitmap(image);
            }
        }
    }

Manifest.xml

<uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NextActivity" />
    </application>

答案 1 :(得分:0)

如果onActivityResult中有条件,请在里面试试。

String pathofpic = getApplication().getExternalFilesDir("MyImages")+ "image_001.jpg"; 
Intent send = new Intent(getActivity(), ShowImage.class);
send.putExtra("picturePath", picturePath);
startActivity(send);

现在在你的ShowImage.class

Intent intent = getIntent();
String pathofpic = getIntent().getStringExtra("picturePath");
yourimageView.setImageBitmap(BitmapFactory.decodeFile(pathofpic));

答案 2 :(得分:0)

摘要

您无需将该URI解析为onActivityResult方法中的文件路径。您可以将该URI对象传递给第二个活动,然后从Intent中提取它并通过setImageUri应用于ImageView。

示范

您的onActivityResult应如下所示。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri selectedImage = data.getData();
    Intent send = new Intent(getActivity(), ShowImage.class);
    send.putExtra("pictureUri", selectedImage.toString());
    startActivity(send);
}

在你的第二项活动中:

iv = (ImageView) findViewById(R.id.showImage);
iv.setImageURI(Uri.parse(getIntent().getStringExtra("pictureUri")));