从相机捕获图像并将其显示在android的另一个活动中
Posted
技术标签:
【中文标题】从相机捕获图像并将其显示在android的另一个活动中【英文标题】:Capturing Image from camera and displaying it in another activity in android 【发布时间】:2014-11-29 04:30:22 【问题描述】:我正在开发一个应用程序,我想从相机捕获图像并将其显示在图像视图的另一个活动中,我的问题是能够捕获图像但捕获后我被重定向到第一个活动而不是第二个。
这是我的代码..
PictureOptions.java
public void buttonCameraOpen(View view)
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
private static Uri getOutputMediaFileUri(int type)
return Uri.fromFile(getOutputMediaFile(type));
/** Create a File for saving an image or video */
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), "Easy Heal");
// 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("MyCameraApp", "failed to create directory");
return null;
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE)
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
else
return null;
return mediaFile;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
Bitmap selectedphoto = null;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK && null!=data)
// Image captured and saved to fileUri specified in the Intent
//selectedphoto = (Bitmap) data.getExtras().get("data");
Uri selectedImage = data.getData();
String [] filePathColumn = MediaStore.Images.Media.DATA;
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
File f =new File(filePath);
String filename = f.getName();
cursor.close();
selectedphoto = BitmapFactory.decodeFile(filePath);
Intent intent = new Intent(PictureOptions.this,ShowImage.class);
//intent.putExtra("data", selectedphoto);
intent.setData( selectedImage );
startActivity(intent);
else if (resultCode == RESULT_CANCELED)
// User cancelled the image capture
else
// Image capture failed, advise user
PictureOptions.xml
<Button
android:id="@+id/buttonCameraOpen"
android:layout_
android:layout_
android:layout_weight="0.35"
android:onClick="buttonCameraOpen"
android:text="@string/button_camera_open" />
ShowImage.java
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_image);
ImageView imageview = (ImageView)findViewById(R.id.ImageShow);
Uri imageUri = getIntent().getData();
//Bitmap selectedphoto =(Bitmap)this.getIntent().getParcelableExtra("data");
imageview.setImageURI(imageUri);
ShowImage.xml
<ImageView
android:id="@+id/ImageShow"
android:layout_
android:layout_ />
【问题讨论】:
抓图后返回第一个activity的onActivityResult是正常的;我看到你在那里调用 startActivity() ,它应该启动第二个活动。您是否在日志中收到任何错误消息? 不,我没有收到任何错误消息,但我想知道为什么我要重定向到第一个活动而不是第二个活动。 图像被捕获并退出相机活动后,您将回到启动活动并调用onActivityResult方法。这是 startActivityForResult() 的正常流程。您可能在 onActivityResult 中遇到错误,导致您无法启动其他活动。 我能够以某种方式打开新活动....现在我的新问题是图像没有传递给其他活动,并且每次都强制关闭。为什么? 如果没有错误日志,就很难猜到......不过有一句话:onActivityResult 中的 data.getData() 会给你一个缩略图,如果你想要“真实”图像,你应该使用'fileUri' 直接传递给相机的路径。 【参考方案1】:我终于找到了一个很棒的解决方案
该库用于从相机中捕获图像或从图库中选择并以onActivityResult
方法返回File
格式的图像,可以在应用程序中进一步使用。
使用
EasyImage Library
【讨论】:
【参考方案2】:Uri uriSavedImage=Uri.fromFile(new File("/sdcard/flashCropped.png"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(camera, 1);
点击图片后,检查是否存在。然后把图片文件的路径发送给下一个activity,通过Bitmap显示出来。
【讨论】:
【参考方案3】:调用相机意图
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
然后在活动上使用这个
'case REQUEST_IMAGE_CAPTURE:
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
saveBitmap(imageBitmap);
mimageView1.setImageBitmap(imageBitmap);
mIntent.putExtra("ACTIVITY_CODE", 1);
startActivity(mIntent);
break;
在你的同一个类中添加这个方法
public void saveBitmap(Bitmap bmp)
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +"/DCIM";
try
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, "time");
cameraUrl=file.getAbsolutePath();
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
catch (Exception e)
Log.e("errr in ","inserting the image at parictular location");
然后在你想要图像的另一个活动中调用它
String valueC = getIntent().getExtras().getString("CAMERA");
Log.v("imageBitmap", ""+valueC);
Bitmap yourSelectedImageC = BitmapFactory.decodeFile(valueC);
mImgV_image.setImageBitmap(yourSelectedImageC);
【讨论】:
【参考方案4】:我花了 2 天后遇到同样的问题,我终于得到了答案
第一个活动
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data != null)
Uri uri = data.getData();
Intent intent= new Intent(this,SecondActivity.class);
selfiSrc.putExtra("imgurl", uri );
startActivity(intent);
第二个活动
Imageview iv_photo=(ImageView)findViewById(R.id.iv_photo);
Bundle extras= getIntent().getExtras();
if(extras!=null)
path = (Uri) extras.get("imgurl");
iv_photo.setImageURI(path);
-
另一种方式看我的回答
Android - how can i transfer ImageView from one activity to another activity?
【讨论】:
以上是关于从相机捕获图像并将其显示在android的另一个活动中的主要内容,如果未能解决你的问题,请参考以下文章