我想拍张照片并保存到画廊。然后从图库中读取这张照片并将其转换为文本(使用 ocr)但是当我尝试用这行代码保存它时,我的应用程序不断崩溃cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);我调试了我的应用程序,发现这条线是导致崩溃的原因。但是没有这条线,我无法保存我的图像。我怎样才能做到这一点 ?package com.example.takepicture;import ...public class MainActivity extends AppCompatActivity { private static final int CAMERA_REQUEST = 1888; ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnCamera = (Button) findViewById(R.id.btnCamera); imageView = (ImageView) findViewById(R.id.ImageView); btnCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); String pictureName = getPictureName(); File imageFile = new File(pictureDirectory,pictureName); Uri pictureUri = Uri.fromFile(imageFile); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri); startActivityForResult(cameraIntent, CAMERA_REQUEST); //capturarFoto(); } }); } private String getPictureName() { String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); return "Img" + timeStamp + ".jpg"; } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_OK) { if (requestCode == CAMERA_REQUEST) { } } }}
1 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
最快的解决方案是添加:
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
打电话之前的某个地方: startActivityForResult(...)
但正确的(对于 API >24)是:
FileProvider.getUriForFile(...)通过and创建 URI
创建提供者(并将其添加到清单文件中)
您可以在此处找到更多信息:https ://stackoverflow.com/a/50265329/5529263
添加回答
举报
0/150
提交
取消