捕获完整尺寸图片时Android应用程序崩溃

问题描述 投票:0回答:2

我正在开发一个Android应用程序,该应用程序可以拍摄照片并将全尺寸的照片保存到设备上。我主要从开发者android网站获得帮助。当我运行代码并尝试拍照时,应用程序立即崩溃。我使用的代码如下:

static final int REQUEST_IMAGE_CAPTURE = 1;

    static final int REQUEST_TAKE_PHOTO = 1;

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                System.out.println("Error in Dispatch Take Picture Intent");
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            //Bundle extras = data.getExtras();
            //Bitmap imageBitmap = (Bitmap) extras.get("data");
            //imageView.setImageBitmap(imageBitmap);
            Toast.makeText(getApplicationContext(),"Working",Toast.LENGTH_LONG).show();
        }
    }
    String currentPhotoPath;

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

    private View.OnClickListener onCamClick() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("Starting Camera Activity");
                //dispatchTakePictureIntent();
                //Toast.makeText(getApplicationContext(),"Working",Toast.LENGTH_LONG).show();

                dispatchTakePictureIntent();

            }
        };
    }

当我运行代码时,出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.zaeem.tia, PID: 15468
    java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.zaeem.tia/files/Pictures/JPEG_20200301_003227_1043176843378455577.jpg
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
        at com.zaeem.tia.app.activities.MainActivity.dispatchTakePictureIntent(MainActivity.java:121)
        at com.zaeem.tia.app.activities.MainActivity.access$000(MainActivity.java:40)
        at com.zaeem.tia.app.activities.MainActivity$1.onClick(MainActivity.java:164)
        at android.view.View.performClick(View.java:7339)
        at android.widget.TextView.performClick(TextView.java:14221)
        at android.view.View.performClickInternal(View.java:7305)
        at android.view.View.access$3200(View.java:846)
        at android.view.View$PerformClick.run(View.java:27787)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7076)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

filepath.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Android/data/com.zaeem.tia.app.activities/files/Pictures" />
</paths>

请对此事提供建议。

java android android-studio
2个回答
0
投票
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);

[这里,您说的是FileProvider的授权字符串是com.example.android.fileprovider。基于该错误,清单中的<provider>元素上没有此错误。


0
投票

将其添加到manifest.xml中(之后):

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="android.getqardio.com.gmslocationtest"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

在Resources / xml上创建provider_paths,并添加以下内容:

<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path
    name="share"
    path="external_files"/>
</paths>

活动:

File imagePath = new File(getFilesDir(), "external_files");
imagePath.mkdir();
File imageFile = new File(imagePath.getPath(), "test.jpg");

// Write data in your file

Uri uri = FileProvider.getUriForFile(this, getPackageName(), imageFile);

Intent intent = ShareCompat.IntentBuilder.from(this)
            .setStream(uri) // uri from FileProvider
            .setType("text/html")
            .getIntent()
            .setAction(Intent.ACTION_VIEW) //Change if needed
            .setDataAndType(uri, "image/*")
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

startActivity(intent);

© www.soinside.com 2019 - 2024. All rights reserved.