I have the following code in my activity that I use to generate a PDF file and save it into the device storage when I click a button I call saveFileToStorageIntent()
The problem that I am finding is that it is not downloaded automatically, instead, this opens another Document provider activity with save option. I want that the pdf file saves directly into Document folder from a single click on my application. What can I do to download the pdf directly to the device?
private fun saveFileToStorageIntent() {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf")
intent.putExtra(Intent.EXTRA_TITLE, "invoice.pdf")
startActivityForResult(intent, CREATE_FILE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CREATE_FILE) {
if (resultCode == RESULT_OK && data != null) {
writePDFToFile(data.data, presenter.ticketResponse()!!)
}
}
}
private fun writePDFToFile(uri: Uri?, body: ResponseBody) {
var inputStream: InputStream? = null
var outputStream: OutputStream? = null
try {
val fileSize = body.contentLength()
val fileReader = ByteArray(fileSize.toInt())
var fileSizeDownloaded: Long = 0
inputStream = body.byteStream()
outputStream = contentResolver.openOutputStream(uri!!)
while (true) {
val read: Int = inputStream.read(fileReader)
if (read == -1) {
break
}
outputStream?.write(fileReader, 0, read)
fileSizeDownloaded += read.toLong()
}
outputStream?.flush()
} catch (e: Exception) {
Logger.print(TAG, e.message)
} finally {
if (inputStream != null) {
try {
inputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
if (outputStream != null) {
try {
outputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
Why are you not using DownloadManager for that?
try{
Uri uri = Uri.parse(downloadPath);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); // Tell on which network you want to download file.
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // This will show notification on top when downloading the file.
request.setTitle("Downloading data..."); // Title for notification.
request.setVisibleInDownloadsUi(true);
request.addRequestHeader("Authorization", "bearer my bearertoken"));
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOCUMENTS, id+".pdf");
((DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE)).enqueue(request); // This will start downloading
}catch(Exception e){
}
You can change the file path to Downloads in the above code.
Related
This below is my code It is working fine but when I am using android 12 it gives crashes ,as it is showing null in onActivityResult method. Can anyone tell me what I am doing wrong.
The intent is getting null and I don't understand why this is happening ,if normal camera intent is used is is working fine but when using this library it is crashes.
enter code here
private fun pickFromGallery() {
CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).start(this#MainActivity)
}
enter code here
private fun createImageFile(imageName: String): File {
val timeStamp: String = SimpleDateFormat("ddMMyyyy").format(Date())
if (SDK_INT > Build.VERSION_CODES.Q) {
directoryPath =
File(Environment.getExternalStorageDirectory().absolutePath + File.separator + "tt1/tt")
} else {
directoryPath = File(
this.getExternalFilesDir(Environment.DIRECTORY_PICTURES + File.separator + "tt1/1tt),
""
)
}
imageDirectoryPath = File(
directoryPath!!.absolutePath,
File.separator.toString() + "${imageName}.jpg"
)
return imageDirectoryPath as File
}
enter code here
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
val result = CropImage.getActivityResult(data)
if (resultCode == RESULT_OK) {
val resultUri = result.uri
img_existingMtr.saveCroppedImageAsync(resultUri)
img_existingMtr.croppedImage
var abc=img_existingMtr.imageResource
Log.e("## cropped", abc.toString())
img_existingMtr.setImageUriAsync(resultUri)
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
val error = result.error
}
}
}
enter code here
private fun storeImage(image: Bitmap) {
val pictureFile: File = createImageFile("firstTest")
if (pictureFile == null) {
Log.e(
"Error",
"Error creating media file, check storage permissions: "
)
return
}
try {
val fos = FileOutputStream(pictureFile)
Log.d("## storeImage", fos.toString())
image.compress(Bitmap.CompressFormat.PNG, 90, fos)
fos.close()
} catch (e: FileNotFoundException) {
Log.d("File1", "File not found: " + e.localizedMessage)
} catch (e: IOException) {
Log.d("File2", "Error accessing file: " + e.localizedMessage)
}
}
I am new to Android development, and im making simple aplication that capture image then save. I can capture the image and show into imageview, but unfortunately i cannot save into gallery.
i tryed many ways but cannot save. this is original code to take and show.
// EDITED //
I add function to save file, but still not working
class TomarFotos : AppCompatActivity() {
lateinit var photoPath: String
val REQUEST_IMAGE_CAPTURE = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_tomar_fotos)
val foto = findViewById<Button>(R.id.tomarfotos)
foto.setOnClickListener{
tomarFoto()
}
}
fun tomarFoto() {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if(intent.resolveActivity(packageManager) != null){
var photoFile: File? = null
try{
photoFile = createImageFile()
}catch (e: IOException){}
if(photoFile != null){
val photoUri = FileProvider.getUriForFile(
this,
"com.example.android.fileprovider",
photoFile
)
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
startActivityForResult(intent,REQUEST_IMAGE_CAPTURE)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val fotoTomada = findViewById<ImageView>(R.id.fotopreview)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
fotoTomada.rotation = 90f
fotoTomada.setImageURI(Uri.parse(photoPath))
}
}
private fun createImageFile(): File? {
val fileName = "foto"
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val image = File.createTempFile(
fileName,
".jpg",
storageDir
)
photoPath = image.absolutePath
return image
}
}
i try developer.android.com already but didnt work
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val fotoTomada = findViewById<ImageView>(R.id.fotopreview)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data?.extras?.get("data") as Bitmap
fotoTomada.setImageBitmap(imageBitmap)
saveImage(imageBitmap)
}
}
Call this function to save image to gallery
private fun saveImage(bitmap: Bitmap) {
var outStream: FileOutputStream? = null
// Write to SD Card
try {
val dir = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "AppName/")
dir.mkdirs()
val fileName = "IMG_${System.currentTimeMillis()}"
val outFile = File(dir, fileName)
outStream = FileOutputStream(outFile)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream)
outStream.flush()
outStream.close()
Toast.makeText(this#MainActivity, "Image Saved Successfully", Toast.LENGTH_LONG).show()
} catch (e: FileNotFoundException) {
Log.d("TAG", "saveImage: ${e.message}")
} catch (e: IOException) {
Log.d("TAG", "saveImage: ${e.message}")
}
}
i'm trying to capture image in android 11 device but request is keep cancelled. i have added also. still unable to capture image. pick image from gallery is working. image is not storing in Android/data folder.
i have added val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) also still unable to create temp file in storage. please provide any solutions
private fun selectImage() {
imageUtils.createImageFile(applicationContext).let {
viewModel.setFileTempPath(it.absolutePath, "doc_name")
startActivityForResult(imageUtils.captureImage(applicationContext, it), 300)
}
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
300 -> when (resultCode) {
RESULT_OK -> {
Log.d("tmppath", data?.data.toString())
if (data != null && data.data != null) {
val selectedImage: Bitmap =
imageUtils.getBitmap(data.data, this#TaggingActivity)!!
viewModel.dataModel.selectedImagesArrayList.value.let {
if (it == null) {
viewModel.dataModel.selectedImagesArrayList.value = ArrayList()
}
val a = it
a?.add(
ImageDetailsToUpload(
"",
selectedImage,
Constant.CUSTOMER_GEO_TAG,
viewModel.dataModel.documentName.value,
viewModel.commonModel.currentLocation.value!!
)
)
viewModel.dataModel.selectedImagesArrayList.postValue(a)
}
} else {
if (viewModel.dataModel.tmpPath.value != null) {
imageUtils.getBitmapFromPath(viewModel.dataModel.tmpPath.value)
?.let { selectedImage ->
val a = viewModel.dataModel.selectedImagesArrayList.value
a?.add(
ImageDetailsToUpload(
"",
selectedImage,
Constant.CUSTOMER_GEO_TAG,
viewModel.dataModel.documentName.value,
viewModel.commonModel.currentLocation.value!!
)
)
viewModel.dataModel.selectedImagesArrayList.postValue(a)
} ?: run {
viewModel.commonModel.showToastTextView("Sorry Try Again..!")
}
} else {
viewModel.commonModel.showToastTextView("Sorry Try Again..!")
return
}
}
viewModel.dataModel.tmpPath.value = null
}
RESULT_CANCELED -> {
viewModel.setFileTempPath("", "")
Toast.makeText(this,"cancelled",Toast.LENGTH_LONG).show()
}
}
}
//Intent class
fun getCameraIntent(file: File): Intent {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file))
if (Build.VERSION.SDK_INT >= 24) {
try {
val m =
StrictMode::class.java.getMethod("disableDeathOnFileUriExposure")
m.invoke(null)
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
} else {
takePictureIntent.putExtra("return-data", true)
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
val pickPhoto = Intent(
Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)
val intents: ArrayList<Intent> = arrayListOf()
intents.add(takePictureIntent)
intents.add(pickPhoto)
val chooserIntent = Intent.createChooser(
intents.removeAt(intents.size - 1),
" Document Upload"
)
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(arrayOf<Parcelable>()))
return chooserIntent
}
My onActivityResult is not working because getBitmap is deprecated. Is there any alternative code to achieve this?
Here is the code that needs to be changed (any suggestions?):
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)
The getBitmap is crossed out in my tooling with a message that says it's deprecated.
This worked for me,
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
val selectedPhotoUri = data.data
try {
selectedPhotoUri?.let {
if(Build.VERSION.SDK_INT < 28) {
val bitmap = MediaStore.Images.Media.getBitmap(
this.contentResolver,
selectedPhotoUri
)
imageView.setImageBitmap(bitmap)
} else {
val source = ImageDecoder.createSource(this.contentResolver, selectedPhotoUri)
val bitmap = ImageDecoder.decodeBitmap(source)
imageView.setImageBitmap(bitmap)
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
This worked well for me in java
ImageDecoder.Source source = ImageDecoder.createSource(this.getContentResolver(), pictureUri);
Bitmap bitmap = ImageDecoder.decodeBitmap(source);
You can use:
private fun getCapturedImage(selectedPhotoUri: Uri): Bitmap {
val bitmap = when {
Build.VERSION.SDK_INT < 28 -> MediaStore.Images.Media.getBitmap(
this.contentResolver,
selectedPhotoUri
)
else -> {
val source = ImageDecoder.createSource(this.contentResolver, selectedPhotoUri)
ImageDecoder.decodeBitmap(source)
}
}
Check the official doc:
This method was deprecated in API level 29.
loading of images should be performed through ImageDecoder#createSource(ContentResolver, Uri), which offers modern features like PostProcessor.
You can use this code for creating bitmap
Bitmap bitmap;
if (Build.VERSION.SDK_INT >= 29) {
ImageDecoder.Source source = ImageDecoder.createSource(getApplicationContext().getContentResolver(), imageUri);
try {
bitmap = ImageDecoder.decodeBitmap(source);
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), imageUri);
} catch (IOException e) {
e.printStackTrace();
}
}
It was evident that the getBitmap API doesn't work with the latest Android SDK - 29. So, this worked for me
Uri contentURI = data.getData();
try {
imageView.setImageURI(contentURI);
} catch (Exception e) {
e.printStackTrace();
}
Please let me know if this doesn't work for any of you, shall other options!
I have created a class for loading a Bitmap from uri:
public class BitmapResolver {
private final static String TAG = "BitmapResolver";
#SuppressWarnings("deprecation")
private static Bitmap getBitmapLegacy(#NonNull ContentResolver contentResolver, #NonNull Uri fileUri){
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(contentResolver, fileUri);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
#TargetApi(Build.VERSION_CODES.P)
private static Bitmap getBitmapImageDecoder(#NonNull ContentResolver contentResolver, #NonNull Uri fileUri){
Bitmap bitmap = null;
try {
bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(contentResolver, fileUri));
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
public static Bitmap getBitmap(#NonNull ContentResolver contentResolver, Uri fileUri){
if (fileUri == null){
Log.i(TAG, "returning null because URI was null");
return null;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
return getBitmapImageDecoder(contentResolver, fileUri);
} else{
return getBitmapLegacy(contentResolver, fileUri);
}
}
}
Just to save you some time ...
ImageDecoder.createSource(this.getContentResolver(), pictureUri)
works fine, but to be able to use this code, mindSdkVersion should be at least 28.
Have you tried this?
val bitmap = ImageDecoder.createSource(contentResolver, uri)
hi freind you check api device
var Image_select: String? = null
var bitmap:Bitmap?=null
you show image set
binding?.ImAvator?.setImageURI(data!!.data)
try {
val uri: Uri? = data!!.data
bitmap = if(Build.VERSION.SDK_INT>=29){
val source: ImageDecoder.Source = ImageDecoder.createSource(requireActivity()
.contentResolver, uri!!)
ImageDecoder.decodeBitmap(source)
} else{
MediaStore.Images.Media.getBitmap(requireActivity().contentResolver, uri!!)
}
} catch (e: IOException) {
e.printStackTrace()
}
when upload image
compress bitmap send server
fun Camparse() {
val size = (bitmap!!.height * (812.0 / bitmap!!.width)).toInt()
val b = Bitmap.createScaledBitmap(bitmap!!, 812, size, true)
val by = ByteArrayOutputStream()
b.compress(Bitmap.CompressFormat.JPEG, 100, by)
val bytes = by.toByteArray()
Image_select = Base64.encodeToString(bytes, 0)
}
For deprecated MediaStore.Images.Media.getBitmap() in API level 29, You can use this code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK) {
if (requestCode == GALLERY_REQUEST) {
Uri selectedImage = data.getData();
try {
if (Build.VERSION.SDK_INT < 29) {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
imageView2.setImageBitmap(bitmap);
} else {
ImageDecoder.Source source = ImageDecoder.createSource(getActivity().getContentResolver(), selectedImage);
Bitmap bitmap = ImageDecoder.decodeBitmap(source);
imageView2.setImageBitmap(bitmap);
}
} catch (IOException e) {
Toast.makeText(getContext(), R.string.error_read_image, Toast.LENGTH_LONG).show();
}
}
}
}
Regards.
if(result.resultCode == Activity.RESULT_OK && result.data != null {
binding?.ivImage?.setImageURI(result.data?.data)}
For anyone getting unsupported bitmap configuration : "Hardware" error or you need mutable bitmap for Canvas or reading pixels use this
ImageDecoder.decodeBitmap(
ImageDecoder.createSource(context.contentResolver, uri)
) { decoder, info, source ->
decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
decoder.isMutableRequired = true
}
This code works for my case:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
try {
when (requestCode) {
//get the image with camera
SECTIONCAMERA -> {
val imageBitmap = data?.extras?.get("data") as Bitmap
ImageView_imagePerfil.setImageBitmap(imageBitmap)
}
//get the image in gallery
SECTIONGALLERY -> {
val imageUri = data?.data
ImageView_imagePerfil.setImageURI(imageUri) }
}
} catch (e: Exception){
e.printStackTrace()
}
}
I need to stub the camera intent by creating a image file at the path provided in the intent extra.
Espresso can only respond with activityresult. Where can i perform the operation to create the file at passed path from intent extra.
Code for launching camera
File destination = new File(Environment.getExternalStorageDirectory(), "app_name" + System.currentTimeMillis() + ".jpg");
imageUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".fileprovider", destination);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, AppConstants.REQUEST_CODE_CAMERA);
Code for stubbing intent in test
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, null);
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);
Ismael answer is perfect. For those looking for solution in java, Here it is.
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(
new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
IntentCallback intentCallback = new IntentCallback() {
#Override
public void onIntentSent(Intent intent) {
if (intent.getAction().equals("android.media.action.IMAGE_CAPTURE")) {
try {
Uri imageUri = intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
Context context = InstrumentationRegistry.getTargetContext();
Bitmap icon = BitmapFactory.decodeResource(
context.getResources(),
R.mipmap.ic_launcher);
OutputStream out = getTargetContext().getContentResolver().openOutputStream(imageUri);
icon.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (IOException e) {
GenericUtility.handleException(e);
}
}
}
};
IntentMonitorRegistry.getInstance().addIntentCallback(intentCallback);
//Perform action here
onView(withId(R.id.tv_take_photo)).perform(click());
You need to create a IntentCallback to intercept the Uri value and save a sample image there.
Sample in Kotlin
intentCallback = IntentCallback {
if (it.action == "android.media.action.IMAGE_CAPTURE") {
it.extras.getParcelable<Uri>("output").run {
val inStream = Resources.getResource(sampleImageFileName).openStream()
val outStream = activity.contentResolver.openOutputStream(this)
ByteStreams.copy(inStream, outStream)
}
}
}
You need to register your callback before the intent trigger event
IntentMonitorRegistry.getInstance().addIntentCallback(intentCallback)
And don't to forget to unregister at end
IntentMonitorRegistry.getInstance().removeIntentCallback(intentCallback)
Ismael and Gupta's answers are correct. For those who want a complete example, I made a complete solution based on their example in Kotlin. The code below takes photo for multiple imageView and also verifies if the correct image is loaded in the respective imageview by checking the imageView.tag property. From the development code, the image name has to be set in the imageView.setTag(imageName)
private var imageName = "No Image Name"
#Test
fun verifyPhotoTaken() {
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(
ActivityResult(Activity.RESULT_OK, null))
takePhoto(R.id.imageview1, R.drawable.ic_launcher)
takePhoto(R.id.imageview2, R.drawable.some_image)
}
private fun takePhoto(imageViewId : Int, resourceId : Int) {
val cameraIntentCallback = intentCallback(resourceId)
IntentMonitorRegistry.getInstance().addIntentCallback(cameraIntentCallback)
onView(withId(imageViewId)).perform(click())
onView(withId(imageViewId)).check(matches(hasDrawable(imageName)))
IntentMonitorRegistry.getInstance().removeIntentCallback(cameraIntentCallback)
}
private fun intentCallback(resourceId : Int = R.drawable.ic_launcher) :IntentCallback {
return IntentCallback {
if (it.action == MediaStore.ACTION_IMAGE_CAPTURE) {
it.extras?.getParcelable<Uri>(MediaStore.EXTRA_OUTPUT).run {
imageName = File(it.getParcelableExtra<Parcelable>(MediaStore.EXTRA_OUTPUT).toString()).name
val context : Context = InstrumentationRegistry.getInstrumentation().targetContext
val outStream = context.contentResolver.openOutputStream(this)
val bitmap : Bitmap = BitmapFactory.decodeResource(context.resources, resourceId)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream)
}
}
}
}
fun hasDrawable(drawableResource: String) : BoundedMatcher<View, ImageView> {
return object : BoundedMatcher<View, ImageView> (ImageView::class.java) {
override fun describeTo(description: Description?) {
description?.appendText("has drawable")
}
override fun matchesSafely(item: ImageView?): Boolean {
return item?.drawable != null && item.tag.toString().contains(drawableResource)
}
}
}