Android - Camera intent not performing save, as mentioned in documentation - java

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));

Related

Share Image to Snapchat

I want to share image to snapchat but giving me "Sorry! You can only share images and videos"
I tried this code but doesnt work Android share image on snapchat
I have this intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.setPackage("com.snapchat.android");
int n = Integer.parseInt(number.getText().toString());
Uri uri = Uri.fromFile(new File(db.getimage(n))); //This is retrieving the Uri path from DB
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Open Snapchat"));
the type that you are setting for the setType method is not correct,
if you want to support any format of Image, I suggest you first get the format of Image And after that pass the correct parameter to the setType() method.
for getting a file extension you can use this link:
link
For sharing data in snapchat, there is a kit available in their official website called Creative Kit.
Also you can find the Doc and Tutorials here.

Android open file input stream from uri

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.

When using Android file provider, files don't have correct permissions despite FLAG_GRANT_WRITE_URI_PERMISSION being flagged in intent

I'm trying to load documents from files in my app using Microsoft Word and PDF viewers and I'm using a FileProvider to handle Android 7.0+ not allowing file URIs to be passed freely. I get the URI like so and and set the Intent flags to allow reading and writing before opening it, like so:
// From the byte array create a file containing that data, and get extension and MIME type.
File fileToOpen = byteArrayToFile(documentData, shortFileName);
String fileExtension = UtilityMethods.getFileExtension(fileToOpen.getName());
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
// Get the Uri for the file from the file provider, open using intent.
Uri documentUri = FileProvider.getUriForFile(getContext(), "com.mycompany.provider", fileToOpen);
Intent intent = new Intent();
intent.setDataAndType(documentUri, mime);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
However when the file loads in MS Word the file is read only, and cannot be edited, which is not the desired behaviour. Where am I going wrong?

ACTION_GET_CONTENT gives wrong path

I'm using ACTION_GET_CONTENT so that the user can select text files that the rest of my code can read and deal with.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("text/*");
startActivityForResult(Intent.createChooser(intent, "select data"), SELECT_DATA);
Above is my code so that the user can browse which works fine.
Uri DataUri = data.getData();
File FileUri = new File(DataUri.getPath());
If I convert DataUri or FileUri to a string after using getPath or getAbsolutePath, I get a completely wrong path.
The path should be /storage/emulated/0/Documents/myFile but it gives me /document/primary:Documents/myFile. I have no idea what this "primary:Documents" thing is.
The data from the intent itself already has the wrong path, any suggestions?
I'm using ACTION_GET_CONTENT so that the user can select text files
No, you are using ACTION_GET_CONTENT so that the user can select some content that has a MIME type of text/*. Where that content comes from is largely up to the user. It may or may not be a file on the filesystem, and if it is, is may or may not be a file that you could access yourself directly (e.g., internal storage of the Dropbox app, files on removable storage on Android 4.4+ devices).
The path should be /storage/emulated/0/Documents/myFile
No, it should not.
but it gives me /document/primary:Documents/myFile
What you get will vary by whatever activity handles the ACTION_GET_CONTENT request. What that activity is will depend on what the user has installed that supports ACTION_GET_CONTENT and what the user chooses out of that list of installed stuff.
Usually, you will get a content Uri (i.e., getScheme() on Uri will return content), as the Uri will point to data being served by a ContentProvider.
The data from the intent itself already has the wrong path
It has the correct path. It is simply not a path on the filesystem.
any suggestions?
Use a ContentResolver and openInputStream() to get an InputStream for the content represented by the content Uri.

Android Camera Intent saving file twice / how to avoid

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.

Categories