用相机拍照并获取图片路径android
Posted
技术标签:
【中文标题】用相机拍照并获取图片路径android【英文标题】:Taking photos with camera and getting picture path android 【发布时间】:2012-08-30 23:54:11 【问题描述】:我的应用程序使用手机相机拍照并将照片存储在手机图库中。然后我想获取该图片路径并将其存储在我的数据存储中。有人可以帮帮我吗?这是我的代码:
public void onClick(View v)
switch(v.getId())
case R.id.btnLoadPic:
//Options for the dialogue menu
final CharSequence[] items = "Camera", "Gallery";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose an Option");
builder.setItems(items, new DialogInterface.OnClickListener()
/**
* Make onclick functionality for the options in the dialogue menu
*/
public void onClick(DialogInterface dialog, int item)
// Camera option
if (item == 0)
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA))
//Toast.makeText(this, "camera", Toast.LENGTH_SHORT).show();
dispatchTakePictureIntent(11);
else
Toast.makeText(null, "No camera avalible", Toast.LENGTH_SHORT).show();
// Gallery option this works fine
private void dispatchTakePictureIntent(int actionCode)
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
//handleSmallCameraPhoto(takePictureIntent);
private void handleSmallCameraPhoto(Intent intent)
Bundle extras = intent.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
ImageView mImageView = (ImageView) this.findViewById(R.id.imagePlayer);
mImageView.setImageBitmap(mImageBitmap);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
handleSmallCameraPhoto(data);
最后一位代码访问相机并在图像视图中显示图片。如何获取字符串格式的图片路径?
【问题讨论】:
浏览这些链接你会得到答案***.com/questions/4184951/…***.com/questions/9092532/… 查看***.com/questions/11927710/… 【参考方案1】:这会有所帮助。经过测试和工作!
public class MainActivity extends Activity
private static final int REQUEST_IMAGE = 100;
private static final String TAG = "MainActivity";
TextView tvPath;
ImageView picture;
File destination;
String imagePath;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvPath = (TextView) findViewById(R.id.idTvPath);
picture = (ImageView) findViewById(R.id.idIvImage);
String name = dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");
destination = new File(Environment.getExternalStorageDirectory(), name + ".jpg");
Button click = (Button) findViewById(R.id.idBtnTakePicture);
click.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
startActivityForResult(intent, REQUEST_IMAGE);
);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK )
try
FileInputStream in = new FileInputStream(destination);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
imagePath = destination.getAbsolutePath();
tvPath.setText(imagePath);
Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
picture.setImageBitmap(bmp);
catch (FileNotFoundException e)
e.printStackTrace();
else
tvPath.setText("Request cancelled");
public String dateToString(Date date, String format)
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(date);
不要忘记在清单文件中添加这些:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
【讨论】:
我刚刚在我的手机上对其进行了测试,在它拍照并尝试保存它之后,它只是强制关闭:( .. 我想可能是我的编码所以我尝试了之前的版本当我在更改之前尝试它并且它也无法正常工作时:( ..现在我的应用程序要求它以前没有做过的硬件许可。这与此有关吗? 你能告诉我它在哪里强制关闭,有什么例外吗?您的设备是否有内置摄像头?(如果没有,它将无法工作)您是否尝试过上面的示例?它适用于我测试的所有设备。如果您的设备没有内置摄像头,则必须进行自定义,在这种情况下,您将需要硬件权限。 @MrPablo 你能详细说明一下吗?你有例外吗? 酷,带有destination.getPath();我们可以找到最后一张相机拍摄(和保存)照片的路径,这正是我所需要的,谢谢。以上是关于用相机拍照并获取图片路径android的主要内容,如果未能解决你的问题,请参考以下文章
Android调用相机实现拍照并裁剪图片,调用手机中的相冊图片并裁剪图片