简体   繁体   English

当我尝试将图像保存到图库时,应用程序不断崩溃

[英]App keep crashing when I try to save image to gallery

I wanted to take a photo and save it to gallery. 我想拍张照片并将其保存到画廊。 Then read this photo from gallery and convert it to text( using ocr) 然后从图库中读取这张照片并将其转换为文本(使用ocr)

But my app keep crashing when I try to save it with this line of code 但是当我尝试使用此行代码保存应用时,我的应用始终崩溃

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

I debug my app and found this line is the responsible for crash. 我调试了我的应用程序,发现此行是导致崩溃的原因。 But without this line I can't save my image. 但是没有这一行,我将无法保存图像。 How can I do that ? 我怎样才能做到这一点 ?

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) {

            }
        }
    }
}

Fastest solution is adding: 最快的解决方案是添加:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

somewhere before calling: startActivityForResult(...) 调用之前的某处: startActivityForResult(...)

But the correct one (for API >24) is: 但是正确的(对于API> 24)是:

  • creating URI via FileProvider.getUriForFile(...) and 通过FileProvider.getUriForFile(...)创建URI和

  • creating provider (and adding it to Manifest file) 创建提供程序(并将其添加到清单文件中)

You can find more info there: https://stackoverflow.com/a/50265329/5529263 您可以在此处找到更多信息: https : //stackoverflow.com/a/50265329/5529263

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM