获取图像的路径在Android [英] Get Path of Image on Android
问题描述
我有一个应用程序,可以使图片并上传。有关上载我必须有maked照片的文件路径。但我不能得到它。
I have an app that can make pictures and upload it. For the upload I must have the file path of the maked photo. But I can't get it.
这是我的code:
public void maakfoto (View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
knop.setVisibility(Button.VISIBLE);
System.out.println(mImageCaptureUri);
}
}
请帮我拿到的文件路径。
Please help me to get the file path.
感谢您的方面
推荐答案
张贴到Twitter需要在设备上的图像的实际路径将在后发送请求。我发现它强大的很难得到实际的路径,更往往不是我会得到错误的道路。
Posting to Twitter needs the image's actual path on the device to be sent in the request to post. I was finding it mighty difficult to get the actual path and more often than not I would get the wrong path.
要对付的是,一旦你有一个位图
,我用它来从使用 getImageUri()$ C $得到URI C>。随后,我用的是
tempUri
Uri实例,以获得实际的路径,因为它是在设备上。
To counter that, once you have a Bitmap
, I use that to get the URI from using the getImageUri()
. Subsequently, I use the tempUri
Uri instance to get the actual path as it is on the device.
这是生产code和自然考验。 ; - )
This is production code and naturally tested. ;-)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
knop.setVisibility(Button.VISIBLE);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
System.out.println(mImageCaptureUri);
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
这篇关于获取图像的路径在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!