How do I attach Ormlite database file to email intent in Android - android

I'm maintaining a legacy Android app that uses OrmLiteSqliteOpenHelper to create a Sqlite database on the device. On the device, I found out this file lives at:
/data/data/[package-name]/databases/mydatabase.db
My app has an "E-mail Support" feature, and I have unfortunately been tasked with attaching this SQLite file to an e-mail Intent for troubleshooting user issues. I'm running into some permission problems. Here is the code I'm using:
public void email(String[] to, String subject ) {
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("*/*");
email.putExtra(android.content.Intent.EXTRA_EMAIL, to);
email.putExtra(android.content.Intent.EXTRA_SUBJECT, subject );
File file = activity.get().getApplicationContext().getDatabasePath("mydatabase.db");
if( file.exists() )
{
if( file.canRead() )
{
Uri uri = Uri.fromFile(file);
email.putExtra(Intent.EXTRA_STREAM, uri);
activity.get().startActivity(Intent.createChooser(email, "Email DB File"));
}
}
}
When I run this code and choose "Gmail" as my mail client, I get a toast on the e-mail client saying "Permission denied for attachment".
Why does this happen? How can I grant permission to Gmail on this file? Any help is appreciated. Thanks.

I have found a workaround for this, but I'm still open to other solutions.
If I create a temporary copy of the file on SD card using Environment.getExternalStorageDirectory(), the Gmail client has permission to read this file. Mail client seems to only have permission issues accessing internal storage.
File file = activity.get().getApplicationContext().getDatabasePath("mydatabase.db");
if( file.exists() && file.canRead() )
{
try {
//We need to make a local copy of the file to SDCard, so Gmail can use it
File destination = new File(Environment.getExternalStorageDirectory(), "database_copy.db");
this.copy(file, destination);
//Attach file and send
Uri uri = Uri.fromFile(destination);
email.putExtra(Intent.EXTRA_STREAM, uri);
activity.get().startActivity(Intent.createChooser(email, "Email DB File"));
}
catch(IOException ioe){
return;
}
}
....
//File copy routine
private void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}

Related

Is READ_EXTERNAL_STORAGE permission ever required to read Uri from Intent.ACTION_GET_CONTENT

I was wondering, if I launch the following Intent.ACTION_GET_CONTENT
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/zip");
startActivityForResult(intent, RequestCode.REQUEST_CHOOSE_BACKUP_FILE);
and try to read the returned Uri from intent in the following way.
Uri uri = data.getData();
// Figure out extension
ContentResolver contentResolver = getContext().getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
final String extension = mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
File temp = null;
try {
temp = File.createTempFile(Utils.getJStockUUID(), "." + extension);
} catch (IOException e) {
e.printStackTrace();
}
// Delete temp file when program exits.
temp.deleteOnExit();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = getContext().getContentResolver().openInputStream(uri);
outputStream = new FileOutputStream(temp);
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
Log.e(TAG, "", e);
} finally {
close(outputStream);
close(inputStream);
}
Is READ_EXTERNAL_STORAGE permission ever required?
I tested a few round. To my surprise, I can perform success read without request for READ_EXTERNAL_STORAGE.
I just would like to confirm READ_EXTERNAL_STORAGE isn't really required to read Uri from Intent.ACTION_GET_CONTENT, in all type of situation.
I have had instances where a user had a third-party file manager installed (File Manager+) and in those cases reading from the Uri returned by ACTION_GET_CONTENT would fail with a permission error if the READ_EXTERNAL_STORAGE permission was not first granted (only if they used the third-party app to select the file, if they used Google Drive or the normal system selection it worked fine without the permission).
I was able to replicate the behavior by installing File Manager+ on one of my emulators with the Play Store and trying it out.

Share audio file from the res/raw folder thorugh Share Intent in Android

I'm trying to share an audio file from my res/raw folder. What I've done so far is:
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound); //parse path to uri
Intent share = new Intent(Intent.ACTION_SEND); //share intent
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share sound to"));
When I choose to share it on GMail, for example, it says something like "Failed to attach empty file". Looks like I'm not getting the right file path, so I'm basically sharing nothing. What am I doing wrong?
Any help would be much appreciated.
Copy the audio file from the resource to external storage and then share it:
InputStream inputStream;
FileOutputStream fileOutputStream;
try {
inputStream = getResources().openRawResource(R.raw.sound);
fileOutputStream = new FileOutputStream(
new File(Environment.getExternalStorageDirectory(), "sound.mp3"));
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
}
inputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/sound.mp3" ));
intent.setType("audio/*");
startActivity(Intent.createChooser(intent, "Share sound"));
Add WRITE_EXTERNAL_STORAGE permission to AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
What am I doing wrong?
Few apps handle android.resource Uri values correctly. Your choices are:
Drop the feature, or
Copy the data from the resource into a file, then use FileProvider, perhaps in conjunction with my LegacyCompatCursorWrapper, or
Use my StreamProvider, which can serve raw resources directly, or
Copy the data from the resource into a file, then use Uri.fromFile(), but this looks like it will stop working with the next version of Android, based on preliminary results from testing with the N Developer Preview
EDIT: It was causing a NullPointException. This is what was I doing:
File dest = Environment.getExternalStorageDirectory();
InputStream in = getResources().openRawResource(R.raw.sound);
try
{
OutputStream out = new FileOutputStream(new File(dest, "sound.mp3"));
byte[] buf = new byte[1024];
int len;
while ( (len = in.read(buf, 0, buf.length)) != -1){
out.write(buf, 0, len);
}
in.close();
out.close();
}catch (Exception e) {}
final Uri uri = FileProvider.getUriForFile(Soundboard.this, "myapp.folagor.miquel.folagor", dest); //NullPointerException right here!!
final Intent intent = ShareCompat.IntentBuilder.from(Soundboard.this)
.setType("audio/*")
.setSubject(getString(R.string.share_subject))
.setStream(uri)
.setChooserTitle(R.string.share_title)
.createChooserIntent()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
The code was just fine. The only problem was that on the Manifest's permisions, I had "WRITE_EXTERNAL_STORAGE" instead of "android.permissions.WRITE_EXTERNAL_STORAGE". So I was not having permision to write in the external storage, which caused a FileNotFoundException due to the lack of permision. Now it works fine!

Android Send Mail with pdf

In My Application I download PDF file to internal storage. after this I want to send mail with the file. I see the file is dowloaded in internal memory
com.my.app -> files-> pdffile.pdf
and it has permissions -rw-------
when I attach to mail the file and send gmail says: could't send attachment. But why ?? I have permission
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
this is code for dowloading file. it runs in async task
public static boolean saveFile(String fileName, Context context){
String fileDirectory = context.getFilesDir().getAbsolutePath()
+ "//"+fileName;
String urlServ = Constants.serverUrl+ "upload/forms/"+fileName;
urlServ = urlServ.replace(" ", "%20");
urlServ = urlServ.replace("\n", "%0d");
urlServ = urlServ.replace("\"", "%22");
int count;
URI fUri = URI.create("file://" + fileDirectory);
File f = new File(fileDirectory);
if (f.exists()){
f.delete();
}
try {
URL url = new URL(urlServ);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(fileDirectory);
//OutputStream output = context.openFileOutput(fileDirectory, Context.MODE_PRIVATE);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
return false;
}
return true;
}
And this is code for sending mail :
public static void sendMail(Context context, String filename) {
String fileDirectory = context.getFilesDir().getAbsolutePath()
+ "/"+filename;
File f = new File(fileDirectory);
Uri URI =Uri.fromFile(f);
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"mytestmail#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_STREAM, URI);
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
context.startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
What is wrong ? it sands mail only with subject and text... maybe there is some permissions issue. How can I donwload file and give it full permissions
EDIT:
It is permission issue, because when I send file with different permission from the same directory, mail with attachment is being sent. -rw-rw-rw- with this permission
How Can i donwload file and set -rw-rw-rw- permission to it ???
I have solved the problem, if anyone faces the same issue. When opening OutputStream like this
it gives new file permission -rw-rw-rw. and other application(Gmail in this case) can use it.
OutputStream output = context.openFileOutput( fileName, Context.MODE_WORLD_READABLE);
The problem here is that although your app has permissions to read the file, the email application doesn't have permission to read the file. Since Jelly Bean, StrictMode has produced a warning when you try to share a File URI outside your application because this kind of problem can occur where the app you are sharing a file with does not have permission to access the file. It is advised to use a content:// URI when sharing files between apps instead of a file:// URI.
I'd suggest using the FileProvider class, which provides a relatively simple way to share your files using a content:// URI.

Android - Share file from sd using bluetooth

Im trying to send a file from my SD using Bluetooth. I'm using Share intent, I wanna send a file from my SD (.mp3). ok when I open the share menu, I can send file to email, dropbox, whatsapp, but if I select Bluetooth, My device shows a message "File null was not sent to ..."
My steps are:
1. Create SEND intent.
2. Copy my file from res/raw to SD
3. Add my file to putExtra
4. Delete the file (is temporal file)
The code:
Intent shareIntent=new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("audio/mp3");
//Copiamos archivo a compartir en la sd
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = sonidoActual+"-temp.mp3";
File newSoundFile = new File(baseDir, fileName);
try {
byte[] readData = new byte[1024*500];
InputStream fis = getResources().openRawResource(contexto.getResources().getIdentifier(sonidoActual,"raw", contexto.getPackageName()));
FileOutputStream fos = new FileOutputStream(newSoundFile);
int i = fis.read(readData);
while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}
fos.close();
} catch (IOException io) {
}
////
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(newSoundFile.getAbsolutePath())/*Uri.parse("file:///sdcard/"+fileName)*//*Uri.parse("android.resource://com.genaut.instantbuttonsfreak/raw/"+texto)*/);
startActivity(Intent.createChooser(shareIntent,getString(R.string.share)));
//
newSoundFile.delete();
Anybody can help me with this? I read a lot but not found a working method, sorry my english.
I think your file is not release by File-I/O.
SO.. try flush() the FileOutPutStream.. like,
fos.flush();
fos.close();
then, use Uri.fromFile(File file) for uri to pass with Intent.. But before passing Uri to Intent just check whether file is exist or not..
like,
if(newSoundFile.exist())
{
shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(newSoundFile))
startActivity(Intent.createChooser(shareIntent,getString(R.string.share)));
newSoundFile.delete();
}

Show PDF in Android

In my onCreate() I do this check:
//
// check if we have a PDF viewer, else bad things happen
//
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("application/pdf");
List<ResolveInfo> intents = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (intents == null || intents.size() == 0) {
// display message then...
finish();
}
On my HTC Desire, this doesn't return a match, even though I have Adobe's PDF viewer. An answer to this question android: open a pdf from my app using the built in pdf viewer mentions that Adobe may not have any public Intents, so the above check will obviously return nothing.
Can anyone verify whether you should be able launch Acrobat from an intent, or is there some other method or PDF viewer to use.
The actual use case is downloading copies of invoices and storing them on local storage using code such as:
URL url = new URL(data);
InputStream myInput = url.openConnection().getInputStream();
FileOutputStream fos = openFileOutput(fname, Context.MODE_WORLD_READABLE);
// transfer bytes from the input file to the output file
byte[] buffer = new byte[8192];
int length;
while ((length = myInput.read(buffer)) > 0) {
fos.write(buffer, 0, length);
progressDialog.setProgress(i++);
}
fos.close();
and then to show
// read from disk, and call intent
openFileInput(fname); // will throw FileNotFoundException
File dir = getFilesDir(); // where files are stored
File file = new File(dir, fname); // new file with our name
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.setType("application/pdf");
startActivity(intent);
Connect your phone to you PC, start Eclipse and open the LogCat. Then download a PDF file with the browser and open it. You should see a line such as (I used the HTC desire):
09-14 17:45:58.152: INFO/ActivityManager(79): Starting activity: Intent { act=android.intent.action.VIEW dat=file:///sdcard/download/FILENAME.pdf typ=application/pdf flg=0x4000000 cmp=com.htc.pdfreader/.ActPDFReader }
Have a go with an explicit intent using the component information. Docs say here:
>
component -- Specifies an explicit name of a component class to use for the intent. Normally this is determined by looking at the other information in the intent (the action, data/type, and categories) and matching that with a component that can handle it. If this attribute is set then none of the evaluation is performed, and this component is used exactly as is. By specifying this attribute, all of the other Intent attributes become optional.
Downside is you will be bound to the htc reader. But you could try an implicit intent first and if that fails try the explicit intent as a fallback.
-Copy the following code in your activity. Call the function CopyReadAssets("File_name.pdf") from onCreate() function. Place the File_name.pdf file in assets folder.
private void CopyReadAssets(String pdfname)
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), pdfname);
try
{
in = assetManager.open(pdfname);
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Toast.makeText(getApplicationContext(), "Pdf Viewer not installed", Toast.LENGTH_SHORT).show();
}
try
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/"+pdfname),
"application/pdf");
startActivity(intent);
}catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), "Pdf Viewer not installed" ,Toast.LENGTH_SHORT).show();
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}

Categories

Resources