我的应用程序使用手机摄像头拍照并将图片存储在手机图库中。我想获得该图片路径并将其存储在我的数据存储区中。有人可以帮帮我吗?继承我的代码:
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);
}
代码的最后几位访问摄像机并在imageview中显示图像。如何以字符串格式获取该图片路径?
答案 0 :(得分:9)
这会有所帮助。经过测试和工作!
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"/>