0

Android でクロップ インテントの使用に問題があります。

アプリの内部ストレージ割り当てに画像を保存していますが、それらをトリミングして壁紙として設定できるようにしたいと考えています。

次のコードを使用して、外部ストレージに保存されている画像で動作させることができます。

cropIntent.setDataAndType(Uri.fromFile(new File(uri)), "image/*");

インテントの下のコードを実行すると、クロップ インテントの起動に失敗し、結果コード 0 が返されます。

Intent cropIntent = new Intent("com.android.camera.action.CROP");

cropIntent.setDataAndType(FileProvider.getUriForFile(activity, "com.example.test.fileprovider", new File(uri)), "image/*");

cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 9);
cropIntent.putExtra("aspectY", 16);
cropIntent.putExtra("return-data", true);
cropIntent.putExtra("scale", true);

activity.startActivityForResult(cropIntent, PIC_CROP);
4

1 に答える 1

4

ストレージ オプションから| Android 開発者:

デフォルトでは、内部ストレージに保存されたファイルはアプリケーション専用であり、他のアプリケーションはそれらにアクセスできません (ユーザーもアクセスできません)。

ACTION_CROPトリミングをサポートする「他のアプリケーション」を起動し、トリミングするファイルの URI を渡します。そのファイルが内部ストレージにある場合、トリミング アプリケーションから直接アクセスすることはできません。

を使用しFileProviderて他のアプリに内部ファイルを提供するには、いくつかの構成が必要です。ファイル共有の設定から| Android 開発者:

FileProvider を指定する

アプリの FileProvider を定義するには、マニフェストにエントリが必要です。このエントリは、コンテンツ URI の生成に使用する機関と、アプリが共有できるディレクトリを指定する XML ファイルの名前を指定します。

<中略>

共有可能なディレクトリを指定する

FileProvider をアプリ マニフェストに追加したら、共有するファイルを含むディレクトリを指定する必要があります。ディレクトリを指定するには、プロジェクトの res/xml/ サブディレクトリに filepaths.xml ファイルを作成することから始めます。

次のFileProviderサンプルはコードを統合し、内部ストレージ内の画像のクロップ アクティビティを正常に開きます (Nexus 5、ストック Android 5.1.1 でテスト済み)。

AndroidManifest.xml

    <application
        ...>
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.test.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
        ...
    </application>

res/xml/filepaths.xml

<paths>
    <files-path path="images/" name="images" />
</paths>

MainActivity.java

            // Manually stored image for testing
            final File imagePath = new File(getFilesDir(), "images");
            final File imageFile = new File(imagePath, "sample.jpg");

            // Provider authority string must match the one declared in AndroidManifest.xml
            final Uri providedUri = FileProvider.getUriForFile(
                    MainActivity.this, "com.example.test.fileprovider", imageFile);

            Intent cropIntent = new Intent("com.android.camera.action.CROP");

            cropIntent.setDataAndType(providedUri, "image/*");

            cropIntent.putExtra("crop", "true");
            cropIntent.putExtra("aspectX", 9);
            cropIntent.putExtra("aspectY", 16);
            cropIntent.putExtra("return-data", true);
            cropIntent.putExtra("scale", true);

            // Exception will be thrown if read permission isn't granted
            cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            startActivityForResult(cropIntent, PIC_CROP);
于 2015-06-21T06:31:22.160 に答える