I am using the easy camera intent for taking simple pictures in Android as follows:
Uri outputFileUri = Uri.fromFile( photo );
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
this.startActivityForResult(camera, CAMERA_RESULT);
The problem is: The file is saved twice
Once when the picture is actually taken (then it is saved to the gallery directory)
Once when the user "accepts" the picture (then it is saved to outputFileUri)
Although I really cant imagine this fact I already read about that the creating of those duplicate files cannot be avoided. But if not, is there a possibility to get the path of the picture in the gallery directory to delete it by my app?
Just to let you guys know, I did some research and it is actually not a good idea to solve it like this - unfortunately.
Nevertheless you can do a very easy SurfaceView-Camera-Activity which will only save the data you are telling it to.
Related
I'm using the following code to let users save a file on Android:
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TITLE, fileName);
startActivityForResult(intent, 2);
where fileName is something like "Drawing 1.ink". The problem is when a file with the same name already exists, a user is suggested to save a new file under "Drawing 1.ink (1)" name. People often save files with "ink (1)" extension.
How to prevent this and make a default suggested file name like "Drawing 1 (1).ink"? Or forcing a suggested name without "(1)"?
How to prevent this
You can't.
Or forcing a suggested name without "(1)"?
You can't.
I want to have control over file extension
You can't.
This is a system-supplied UI, not significantly different from platform-supplied "file save-as" dialogs that we have used for decades. You're welcome to file feature requests to improve the available options here, though such changes would only take effect with Android 12 or some other future version.
I'm trying to upload a file by copying it from mobile app to server app running on windows.
I used a file chooser to let the user select the file:
public void openFile(View view) {
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file"), LEARN_TREE);
}
Then i got the uri from the intent of onActivityResult.
First question here is why it displays a file named "servo.dat" as numbers (in this case it shows "5889")?
After that I put the uri as an extra into another intent and use that intent to start another activity.
In the second activity I retrieve the uri.
Now I'd like to use FileInputStream to read bytes from my file in order to write them to the ObjectOutputStream created from Socket.getOutputStream().
Here is where it doesn't work. Basically the path provided here
FileInputStream fis = new FileInputStream(uri.getPath());
is incorrect. If I check on my device the file location is Download/servo.dat, the Uri in the app shows Download/5889 and the absolute path that I tried retrieving using a UriUtils library found online shows storage/emulated/0/Download/servo.dat but this one doesn't actually exist on my phone.
I think it's not so hard but I'm getting confused since I'm new to both Android app development and Android itself, please help!
I'm open to any good solution, I saw online there is the ContentResolver class that should be helpful but I didn't manage to understand how to use it :|
First question here is why it displays a file named "servo.dat" as numbers (in this case it shows "5889")?
Because it is not a file. It is a piece of content, and you are attempting to treat the path portion of a Uri as a filesystem path, which is is not.
If you want a display name for the content:
Call DocumentFile.fromSingleUri(), passing in your Uri, to create a DocumentFile
Call getDisplayName on the DocumentFile
Basically the path provided here is incorrect
That is because you are trying to treat the path portion of a Uri as a filesystem path, which it is not.
To get an InputStream, call openInputStream() on a ContentResolver, passing in your Uri. See the documentation. So, for example, from a method in an Activity, you would use InputStream inputStream = getContentResolver().openInputStream(uri);.
I'm getting confused since I'm new to both Android app development and Android itself
You may wish to consider reading a book on Android app development or taking a course in Android app development.
The question is, how do I send a large image (example 720 * 8549) to the WhatsApp without compression and loss of quality.
Is it possible to do this?
Below is the code to send the image that I use:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
File photoFile = new File(imageFile.getParent(), imageFile.getName());
Uri photoURI = FileProvider.getUriForFile(getBaseContext(), getApplicationContext().getPackageName(),
photoFile);
shareIntent.putExtra(Intent.EXTRA_STREAM,photoURI);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, getBaseContext().getString(R.string.photo_share)));
So I send a compressed and unreadable image.
Maybe someone came across a similar.
Good luck to all!
This is not possible because WhatsApp always reduces the size. Google Photos also tried this but if you choose Original when sending over Google Photos the size is still shrinked in the end.
Why do you not take another app like Telegram? You have much more options there.
The easiest way would be to share a link to the original e.g. Google Photos or Dropbox.
I've been dealing with a problem regarding the sharing of images in an Android app. I've been told it's easier to save the image I want to share in the content provider. I've done that like this: ( don't know if i am doing it right )
File auxFile = new File(getContext().getFilesDir(), imgName);
if (!auxFile.exists()){
FileOutputStream fos = new FileOutputStream(auxFile);
fos.write(bytesDecrypted);
fos.flush();
fos.close();
}
Once that is done I try to share the image through a chooser intent like this:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
Log.v("CachedProvider",CachedFileProvider.AUTHORITY);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + CachedFileProvider.AUTHORITY + File.separator + imgName));
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent,"share"));
In the app I have implemented a method that can receive Images from other apps sharing it ( that part actually work ) so I decided to test my sharing "button" with my own app but it actually don't work.
For the moment I have not set any special provider on my manifest as I don't know if that is necessary.
If further information is needed please feel free to ask.
Thanks you all very much in advance!
Edit:
When I click the "button" a "chooser" between the diferent apps that accept images is beeing displayed. Once I choose the app that I want to share the image with I get no responce.
I have a camera intent set up to try to create a file in the root of my device.
File storagePath = new File(Environment.getExternalStorageDirectory()+ "/Football_Fans");
storagePath.mkdirs();
File file = new File(storagePath, "FAN_IMAGE_TEMP");
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, storagePath);
startActivityForResult(cameraIntent,CAMERA_REQUEST_IMAGE);
When I run my application, I don't have any activityOnResult set, but I use fileExplorer to try to see if my file was created. My folder gets created fine, but the photo does not show up. Any idea why?
The documentation states that if an EXTRA_OUTPUT is set, it will write to that location. So I'm confused why it's not working.
The caller may pass an extra EXTRA_OUTPUT to control where this image
will be written. If the EXTRA_OUTPUT is not present, then a small
sized image is returned as a Bitmap object in the extra field. This is
useful for applications that only need a small image. If the
EXTRA_OUTPUT is present, then the full-sized image will be written to
the Uri value of EXTRA_OUTPUT.
use Uri. try this
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(storagePath));
"..will be written to the Uri value of EXTRA_OUTPUT."
You are passing in a File object (storagePath). It expects a Uri so use this:
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(storagePath));