我正在开发一个Android应用程序。它具有捕获图像应显示在屏幕上的功能,我需要获取该图像路径,以便我可以将该图像发送到服务器。
我可以在屏幕上显示图像,但无法获得绝对图像路径。
我的路径如下:
content://media/external/images/media/1220
content://media/external/images/media/1221
如何获得实际图像路径?
这是我的代码:
mintent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mintent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
startActivityForResult(mintent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((data != null && !data.toString().equalsIgnoreCase("Intent { }"))
|| requestCode == 1)
switch (requestCode) {
case 1:
try {
// Uri imageFileUri = data.getData();
// String path=getRealPathFromURI(this,imageFileUri);
// Bitmap bitmap = (Bitmap) data.getExtras().get("data");
String photoPath = null;
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Intent intent = new Intent(this, Media.class);
intent.putExtra("BitmapImage", bitmap);
Bundle b = mintent.getExtras();
if (b != null && b.containsKey(MediaStore.EXTRA_OUTPUT)) { // large
// image?
String timeStamp = new SimpleDateFormat(
"yyyyMMdd_HHmmss").format(new Date());
// Shouldn't have to do this ... but
photoPath = MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap, "Img" + timeStamp
+ ".jpeg", null);
}
}
}
}
photoPath
值显示在上面。
如何从此路径获取绝对图像路径。
答案 0 :(得分:11)
这是一个有效的功能 -
String selectedImagePath = getPath(selectedImageUri);
private String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
答案 1 :(得分:1)
完整的解决方案
final private int PICK_IMAGE = 1;
final private int CAPTURE_IMAGE = 2;
private String imgPath;
btnGallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
}
});
btnCapture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAPTURE_IMAGE);
}
});
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == PICK_IMAGE) {
selectedImagePath = getAbsolutePath(data.getData());
imgUser.setImageBitmap(decodeFile(selectedImagePath));
} else if (requestCode == CAPTURE_IMAGE) {
selectedImagePath = getImagePath();
imgUser.setImageBitmap(decodeFile(selectedImagePath));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
public String getAbsolutePath(Uri uri) {
String[] projection = { MediaColumns.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
答案 2 :(得分:-2)
在相机按钮上
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, CAMERA_REQUEST);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
和活动结果写
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
// Bitmap photo1 = (Bitmap) data.getExtras().get("data");
File file1 = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
Bitmap bitmap11 = decodeSampledBitmapFromFile(file1.getAbsolutePath(), 500, 300);
//
}
}