Java 无法获取捕获的图像uri
我正在尝试获取捕获的图像Uri,但没有获取下面的代码来说明如何获取Uri。Toast消息给我空结果Java 无法获取捕获的图像uri,java,android,android-camera-intent,Java,Android,Android Camera Intent,我正在尝试获取捕获的图像Uri,但没有获取下面的代码来说明如何获取Uri。Toast消息给我空结果 Button b=findViewById(R.id.button); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i=new Intent(MediaStore.ACTION_IMA
Button b=findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(Authentication.this,""+data.getData(),Toast.LENGTH_LONG).show();
}
试试这个:
if (requestCode == CAPTURE_IMAGE &&
resultCode == RESULT_OK) {
if (data != null) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Uri imageUri = getImageUri(getApplicationContext(), bitmap);
}
}
调用此方法以获取uri fom位图:
public Uri getImageUri(Context inContext, Bitmap myImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(),
myImage, "Title", null);
return Uri.parse(path);
}
试试这个:
if (requestCode == CAPTURE_IMAGE &&
resultCode == RESULT_OK) {
if (data != null) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
Uri imageUri = getImageUri(getApplicationContext(), bitmap);
}
}
调用此方法以获取uri fom位图:
public Uri getImageUri(Context inContext, Bitmap myImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(),
myImage, "Title", null);
return Uri.parse(path);
}
从Android版本的棒棒糖中,出于安全原因,无法直接获取URI-
private static Uri getSharableImageUri(Context context) {
File outputDir = context.getCacheDir();
File imagePath = new File(outputDir, context.getString(R.string.app_name));
if (!imagePath.exists()) {
imagePath.mkdir();
}
File tempFile = new File(imagePath, "image.jpg");
Uri uri = null;
try {
if (!tempFile.exists()) {
tempFile.createNewFile();
}
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", tempFile);
} catch (Exception e) {
e.printStackTrace();
}
return uri;
}
在按钮的“单击侦听器”中使用-
Button b=findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mImageCaptureUri = getSharableImageUri(context);
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
intent.setClipData(ClipData.newRawUri("", mImageCaptureUri));
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, 2);
} catch (Exception e) {
e.printStackTrace();
}
}
});
从Android版本的棒棒糖中,出于安全原因,无法直接获取URI-
private static Uri getSharableImageUri(Context context) {
File outputDir = context.getCacheDir();
File imagePath = new File(outputDir, context.getString(R.string.app_name));
if (!imagePath.exists()) {
imagePath.mkdir();
}
File tempFile = new File(imagePath, "image.jpg");
Uri uri = null;
try {
if (!tempFile.exists()) {
tempFile.createNewFile();
}
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", tempFile);
} catch (Exception e) {
e.printStackTrace();
}
return uri;
}
在按钮的“单击侦听器”中使用-
Button b=findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mImageCaptureUri = getSharableImageUri(context);
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
intent.setClipData(ClipData.newRawUri("", mImageCaptureUri));
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, 2);
} catch (Exception e) {
e.printStackTrace();
}
}
});
您将无法获得uri。通过这种方式,您将获得具有此目的的位图。@aj pawar。我想你需要检查一下这个答案。blackapps是对的,你在讨论中提到的代码会给你位图,你不会得到uri。通过这种方式,您将获得具有此目的的位图。@aj pawar。我想你需要检查一下这个答案。blackapps是对的,您刚才提到的代码将为您提供位图,但是先生,我想在我们需要的时候将图像存储到firebase_存储中Uri@AjPawar您需要文件提供程序。然后你可以获取文件uri并上传到firebase存储。但先生,我想在我们需要的时候将图像存储到firebase_存储Uri@AjPawar您需要文件提供程序。然后您可以获取文件uri并上传到firebase存储。