gpt4 book ai didi

android - 相机新拍摄的图像路径总是返回空

转载 作者:行者123 更新时间:2023-11-30 04:55:32 25 4
gpt4 key购买 nike

我正在构建一个应用程序,它的功能之一是拍照并将其上传到服务器。

这是我的做法。我使用 reqCode = 7

btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, reqCode);
}
});

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getActivity(), requestCode + " - " + resultCode, Toast.LENGTH_LONG).show();

if(requestCode == reqCode && resultCode == RESULT_OK)
{
bitmap = (Bitmap) data.getExtras().get("data");
imageCamera.setImageBitmap(bitmap);

Uri tempUri = getImageUri(getActivity().getApplicationContext(), bitmap);

//Toast.makeText(getActivity(),"Here "+ tempUri, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getActivity(), "Failed To Capture Image", Toast.LENGTH_LONG).show();
}
}

private Uri getImageUri(Context applicationContext, Bitmap photo) {
Bitmap OutImage = Bitmap.createScaledBitmap(photo, 1000, 1000,true);
String path = MediaStore.Images.Media.insertImage(applicationContext.getContentResolver(), OutImage, "Title", null);
return Uri.parse(path);
}

奇怪的是,getImageUri 中的字符串路径总是返回空。通过相机获取最近拍摄的图片的图像路径的正确方法是什么?正确的请求代码是什么?

最佳答案

File file;
Uri fileUri;

final int RC_TAKE_PHOTO = 1;

private void takePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(getActivity().getExternalCacheDir(),
String.valueOf(System.currentTimeMillis()) + ".jpg");
fileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
getActivity().startActivityForResult(intent, RC_TAKE_PHOTO);

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RC_TAKE_PHOTO && resultCode == RESULT_OK) {

//do whatever you need with taken photo using file or fileUri

galleryUpdatePic(tmpFileUri.getPath());

Glide.with(mContext)
.load(tmpFileUri.getPath())
.into(img_profile_user);

}
}
}




private void galleryUpdatePic(String mediaUrl) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mediaUrl);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}

关于android - 相机新拍摄的图像路径总是返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59265095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com