How to get file bytes with Dropbox Chooser for Android - android

I am using Dropbox chooser for Android.
mDropboxChooser.forResultType(DbxChooser.ResultType.FILE_CONTENT)
.launch(SendActivity.this,
DBX_CHOOSER_REQUEST_CODE);
and in onActivityResult I get the url
DbxChooser.Result result = new DbxChooser.Result(data);
Uri u = result.getLink();
but I am not sure how can I get file bytes using this Uri.

Adding Andy Res's answer as an answer, because he's right. :-)
I think you are supposed to use that Uri to open an input stream
connection, that is from where you'll get the bytes, presumably making
a HttpUriRequest.
EDIT
Also note that you should get a "direct" link, not a "preview" link if you want to download the file contents.

Related

How to get actual path to video from onActivityResult

I have a video that I save to .../Movies/MyApp/abcde.mp4. So I know where it is. When I load it through my app using an implicit intent to ACTION_GET_CONTENT, the path is returned as content:/media/external/video/media/82 when I do
data.getData().toString()
The problem with that path is that it works when I try to access it with MediaRecorder as
mVideoView.setVideoURI(Uri.parse(mVideoStringPath))
However if I try to convert it to a path in another thread (for a job queue), the file is not found
new File(mVideoStringPath)
when I use the technique (copy and paste) described at How to get file path in onActivityResult in Android 4.4, still get the error
java.lang.RuntimeException: Invalid image file
Also per my logging, the new technique shows the path to the video as
video path: /storage/emulated/0/Movies/MyApp/abc de.mp4
notice the space in abc de.mp4. that indeed is the name of the file. And the phone's camera app has no trouble playing
However if I try to convert it to a path in another thread (for a job queue), the file is not found
That is because it is not a path to a file. It is a Uri, which is an opaque handle to some data.
How to get actual path to video from onActivityResult
You don't. You use the Uri. There is no requirement that the Uri point to a file. There is no requirement that the Uri, if it happens to represent a file, represent one that you have direct filesystem access to.
you need to escape the space the the file path in order to construct a File object from it.
filepath.replace(" ", "\\ ");

Android how to pass a PDF file as byte array to an intent

I have an activity which allows users to open encrypted pdf files with a default pdf/ebook viewer. The result of the decryption of an encrypted pdf file is a byte array. I want to pass that byte array to an intent which opens that byte array as a PDF. The intent instantiation is something like this:
PackageManager pm = PackageManager;
Intent intentPdf = pm.GetLaunchIntentForPackage("com.adobe.android");
Is this possible? Can the pdf reader (Adobe/any other) understand from a byte array that it's a PDF file?
I tried with Bundle.PutByteArray and Intent.putExtra but it won't work.
var bundle = new Bundle();
bundle.PutByteArray("key", inputFileData);
intentPdf.PutExtra("name", bundle);
StartActivity(intentPdf);
inputFileData is my byte array.
Can the pdf reader (Adobe/any other) understand from a byte array that it's a PDF file?
Probably not. You are welcome to contact the developers of those apps and ask them. And, as others have pointed out, this is a bad idea due to size issues. Also, if the PDF viewer you launch happens to start its own task, your byte array will be readable by any app on the device.
Or, you are welcome to use a ContentProvider, like this one, where you work out some single-use Uri structure, so once the PDF viewer uses the Uri, nothing else can.
Or, you are welcome to write your own PDF viewer that works off of a byte array.
I think the max size of a bundle is quite small, you would be best off passing a URI to the pdf reader of the decrypted pdf.

How to attach more than one image and Audio recording file with Image body in Android?

I am new in Android I am trying to send more than 1 images in email body as well as audio recording file. The audio recording file is stored in sdcard as well as images are also stored in sdcard OR I click that using Camera.
I searched on this particular problem and I found that so many people are suffered with the same problem but I dont found the exact answer/ solution.
Can anyone tell me the correct answer. I post my code so that you all people can suggest me my mistake.
Here I want to attach 4 images. In the StringBuffer() I append the path of all images.
Please help me I am stuck on this. As well as suggest me that How Can I attach Audio file in the mail?
Thanks.
StringBuffer() strImages = new StringBuffer();
strImages.append(strImg1_pref);
strImages.append("\n\n");
strImages.append(strImg2_pref);
strImages.append("\n\n");
strImages.append(strImg3_pref);
strImages.append("\n\n");
strImages.append(strImg4_pref);
strImages.append("\n\n");
String strTemp = strImages.toString();
startActivity(Intent.createChooser(new Intent(Intent.ACTION_SEND).setType("image/jpeg").setType("message/rfc822")
.putExtra(Intent.EXTRA_EMAIL, emails)
.putExtra(Intent.EXTRA_SUBJECT, subject)
.putExtra(Intent.EXTRA_TEXT, strDetails).putExtra(Intent.EXTRA_STREAM, Uri.parse(strTemp)), "Send your email in:"));

Android: Content type error using ACTION_VIEW with a local file

I'm trying to post a notification that lets the user open a locally stored file. My code looks like this:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(filename));
notificationIntent.setData(uri);
Where "filename" is the full path to a locally stored file, usually in the /mnt/sdcard/download directory. The files I want to display are of various types: images, PDF documents, HTML, etc.
This works, but sometimes Android tries to open the file as the wrong type. For example, a jpeg file will open in a web browser view and instead of seeing the image, I see the binary data from the file displayed as text. Other times it works file. For example, some PDF files correctly open in a PDF viewer and some do not.
I'm not sure why this is. The documentation says I should not have to pass an explicit content type. If I do set the content type explicitly, things seem to work fine. The problem is, I don't always know what the content type should be (the file is downloaded from an external source and can be anything, and no, the MIME type is not in the HTTP headers, I checked for that).
What can I do here? Is there some function I can call with a filename to have Android return me the best content type for that file? Moreover, why is this not happening automatically when the Intent is processed?
Thanks.
You've most likely figured this out; I'm posting in case someone else is stuck on this. I do the following to get the mime-type of the file:
//Get the file path
Uri path = Uri.fromFile(file);
MimeTypeMap type_map = MimeTypeMap.getSingleton();
//Get the extension from the path
String extension = MimeTypeMap.getFileExtensionFromUrl(path.toString());
extension = extension.toLowerCase();
if (extension.contains(".")) {
extension = extension.substring(extension.lastIndexOf("."));
}
String mime_type = type_map.getMimeTypeFromExtension(extension);

Converting string to email-attachment on android

So I have som data that I have converted to a string. While I have found how to attach something from the SD-card to a mail, I cant figure out how to directly convert my string to a mail-attachment without involving the SD-card. In case it holds significance, I have read some data from a database, converted it to csv-format, and now wants to attach it as a csv-file.
Cheers,
I have the same problem, i try to send a file without saving it in filesystem.
I tried to add a data uri al stream extra like this:
String fileContent = "File Content";
emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("data://text/plain;base64,"+
_utils.Strings.base64_encode(fileContent)));
I used my own class to create the base64 content, but i think it will also work with:
http://developer.android.com/reference/android/util/Base64.html
It was a success in that case, that saw the atached "file" in my e-mail client.
But there are 2 problems:
1. I don`t know how to define a name for this file
2. an error occours when i try to send the file and I get a mail without attachment at the other end.
UPDATE 2015-05-13:
The mail app displays following error for my attachent:
E/Gmail(11511): java.io.FileNotFoundException: No content provider: data://text/plain;base64,
I think that means my phone is yust missing an content provider which can handle data uris.
So I think we have to create ContentProvider (see: http://developer.android.com/guide/topics/providers/content-provider-creating.html)
And implement a
openOutputStream(android.net.Uri)
to return the content of the data uri.
create a temporary file using the file API's and then you can go ahead and put it as extra in the email intent like this
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(csvFile));

Categories

Resources