我花了几天时间阅读文档并遵循各种教程,但无论我做什么,我都会遇到错误。我正在尝试将图像保存在内部存储中,然后将其转换为内容 Uri,以便我可以使用系统裁剪和调整大小功能。如果有人能发现我的代码哪里有问题,我将不胜感激:
主要活动:
new Thread(() -> {
try {
String temp = "temp";
//Convert bitmap to byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] bitmapData = bos.toByteArray();
//Write the bytes in file
FileOutputStream fos = openFileOutput(temp, MODE_PRIVATE);
fos.write(bitmapData);
fos.flush();
fos.close();
//onvert file to contentUri
File file = new File(getFilesDir(), temp);
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Uri imageUri = FileProvider.getUriForFile(MainActivity.this, "@string/file_provider_authority", file);
intent.setDataAndType(imageUri, "image/jpg");
intent = WallpaperManager.getInstance(getApplicationContext()).getCropAndSetWallpaperIntent(imageUri);
startActivityForResult(intent, ACTIVITY_CROP);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
显现:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="@string/file_provider_authority"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
</application>
file_provider_paths.xml:
<paths>
<files-path
path="/" name="temp" />
字符串.xml:
<resources>
<string name="file_provider_authority"
translatable="false">com.me.wallpaper_app.provider
</string>
繁花如伊
慕斯王
相关分类