Share mp3 file from Storage to specific whatsapp user - java

private void share(Uri uri){
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("audio/*");
waIntent.putExtra("jid", get_DATA.getSelectedPhonewdcountrycode() + "#s.whatsapp.net"); //phone number without "+" prefix
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(waIntent, "Share with"));
}
I have tried many solution but every solutio takes me to whatsapp contact list to chose a contact, i want the file to be send directly to users inbox
BTW it does take me to the targeted contact after a few second delay but it toast a message File is not supported

There is no foreseeable way to upload it directly from a file to a user (unlike on laptops), unless you extend the file path to reach a particular contact e.g
com.whatsapp.jeremy
This however might still not help as WhatsApp chats are end to end encrypted, not direct, so you might have to go through some of the database files like db.crypt to find the right path to each individual.
Try seeing how these pan out for you

Related

Prevent extension prefix (1) when saving a file on Android

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.

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.

Check whether a known document Uri exists in android phone storage

I am fetching a document Uri from the server which points out to where the document (pdf or word) was initially stored in the phone storage at the time of upload. In my app, I would want to check if the document still exists in the user's phone storage (by checking if this uri is still valid) before downloading the document. How can I do this?I've tried multiple ways to no success. Please help.
This is what I've already tried.
Uri uri = Uri.parse("content://com.android.providers.downloads.documments/document/748");
final File f = new File(uri.getPath());
if (f.exists()){
Log.d("File existence", "Yes, file exists");
}else {
Log.d("File does not exist", "so sorry");
}
I also tried implementing this using ContentResolver as pointed out in this similar question but the method threw a security exception: querying the cursor requires manage documents permission which when added to the manifest also brought an error, that the permission is only used by system apps.
Please help. I'm running the app in android 6.0.1
Thanks in advance
Try this out :
getContext().getContentResolver().getPersistedUriPermissions().forEach( {element -> element.uri == yourUri});

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.

Let your app attach files to email etc

My application lets the user save files, but now i want to give the user the possibility to get out of my app and share the files. So i created an intend. If you do this, Android lets you select the way of exporting the file. If you choose e-mail, a new mail with attachment and attachment size will appear. But if i send the mail, i won't receive any attachments.
I'm afraid this happens because the mail app is not able to access the files in my app's folder. If this is the reason, is there an other way? I didn't find any information about this.
This is the code:
File file = new File(getFilesDir().toString()+"/"+longClickItem);
if (file.exists() == true) {
Intent sendIntent = new Intent();
sendIntent.setType("text/plain");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL, getListView().getCheckedItemIds());
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(sendIntent);
} else {
...
}

Categories