我现在尝试了几天,但我似乎无法得到它。 这是我的代码,用于启动相机并发送到另一个活动。
@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);
请有人帮帮我,这是杀了我!
答案 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")));