我正在尝试在另一个活动图像视图上显示从相机单击的图片,但是将图像从捆绑包中取出到位图中..我收到此错误。我的代码是
package com.example.iwm;
import java.io.File;
import java.text.SimpleDateFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
//private static final int RESULT_OK = -1;
private static final int MEDIA_TYPE_IMAGE = 1;
public static Bundle s = null;
//public static String y ;
private Uri fileUri;
private static Intent intent;
private static Intent intent2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create Intent to take a picture and return control to the calling application
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(android.provider.MediaStore.ACTION_IMAGE_CAPTURE, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
//onActivityResult(CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE, int resultCode, intent)
}
private Uri getOutputMediaFileUri(int type) {
// TODO Auto-generated method stub
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
@SuppressLint("SimpleDateFormat")
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "IWMP-Images");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("IWMP-Images", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + fileUri, Toast.LENGTH_LONG).show();
s =data.getExtras();
intent2 = new Intent(MainActivity.this,MainActivity2.class);
intent2.putExtra("Image", s);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent2);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
Toast.makeText(this, "Image NOT saved ", Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
`
从此我将数据(意图)保存在包 S 中并将其发送到 activty2
我的activity2代码是
package com.example.iwm;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity2 extends Activity {
TextView tx;
Bitmap bmp;
ImageView ivUserImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
ivUserImage = (ImageView)findViewById(R.id.image);
tx = (TextView)findViewById(R.id.text1);
//tx.setText(MainActivity.y);
Bundle bundle = getIntent().getExtras();
try
{
bmp =(Bitmap)bundle.get("Image");
}
catch(Exception e)
{
tx.setText(e.toString());
}
ivUserImage.setImageBitmap(bmp);
//int i=10;
}
public void upload(View view)
{
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_activity2, menu);
return true;
}
}