In this Document Scanner App, I accessed the storage photos and import to my app as well, But I also want to Write External Storage to create a folder in External Storage where I can Store the documents. Only a single permission dialogue box appears which asks for the permission to access photos, the other one which is about Allow User to Manage Storage, does not appears.
I tried to get Read and Write External Storage (Manage External Storage), I can read the storage but Unable to Manage it for creating a folder in Local Storage. Without Manage External Storage the app crashes while saving the document. I also tried to get permissions manually in app info settings and it works well. But It is required to get permissions from app for a user.
`
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
setupScanner()
}
}
@Composable
fun setupScanner() {
val options = GmsDocumentScannerOptions.Builder()
.setGalleryImportAllowed(true)
.setResultFormats(RESULT_FORMAT_JPEG, RESULT_FORMAT_PDF)
.setScannerMode(SCANNER_MODE_FULL)
.build()
val scanner = GmsDocumentScanning.getClient(options)
Text(text = "Welcome to ScanWush")
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.background
) {
var imageUris by remember {
mutableStateOf<List<Uri>>(emptyList())
}
val scannerLauncher =
rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartIntentSenderForResult(),
onResult = {
if (it.resultCode == RESULT_OK) {
val result =
GmsDocumentScanningResult.fromActivityResultIntent(it.data)
imageUris = result?.pages?.map { it.imageUri } ?: emptyList()
result?.pdf.let { pdf ->
val docName = UUID.randomUUID().toString()
val folderName = "ScanWush"
val folder =
File(Environment.getExternalStorageDirectory(), folderName)
if (!folder.exists()) {
folder.mkdir()
}
val file = File(folder, "$docName.pdf")
val fos = FileOutputStream(file)
pdf?.let { it1 ->
contentResolver.openInputStream(it1.uri)?.use {
it.copyTo(fos)
Toast.makeText(
this,
"Saved to ScanWush Folder",
Toast.LENGTH_SHORT
).show()
}
}
}
}
}
)
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
AsyncImage(
model = imageUris,
contentDescription = null,
contentScale = ContentScale.FillWidth,
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(16.dp)) // Add some space between the image and the button
Text(text = "Welcome to ScanWush")
Spacer(modifier = Modifier.height(16.dp)) // Add some space between the image and the button
Button(onClick = {
if (ContextCompat.checkSelfPermission(
this@MainActivity,
android.Manifest.permission.READ_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(
this@MainActivity,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(
this@MainActivity,
android.Manifest.permission.MANAGE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this@MainActivity,
arrayOf(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.MANAGE_EXTERNAL_STORAGE
),
1
)
} else {
scanner.getStartScanIntent(this@MainActivity).addOnSuccessListener {
scannerLauncher.launch(
IntentSenderRequest.Builder(it).build()
)
}.addOnFailureListener {
Toast.makeText(
applicationContext,
it.message,
Toast.LENGTH_SHORT
).show()
}
}
}, modifier = Modifier.background(Color.White)) {
Text(text = ("Scan PDF"))
}
}
}
}
}`