send raw image or binary data directly to the other application - android

I would like to ask if there is a possibility to send raw data to the other application? Example: taking pictures from a custom camera and send to other application. I observe that using intent is just only sending data which is already saved to storage, but i want that let the 2nd application decided either they save the image or discard. Just like in the tutorial of on how to make custom camera but in that instance, only one application running and only startActivityForResult and the listener which is onResultActivity. In my case, I want that from my custom camera, pass the raw data to the other application and let the other application manage the raw image receive. I call this raw image because this image is in the buffer and not on the storage. Is that possible to pass the raw image/data to other application?

You would need to save the file and pass the URI to the file via an Intent.
Uri image = Uri.parse(fileName);
Intent myIntent = new Intent(Intent.ACTION_VIEW, image);
myIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(myIntent, "Open Image");
startActivity(chooserIntent);

Related

How to pass a JPEG image to another activity using bundle?

I'm now writing an app using android studio. In the main activity, I wrote a function to take a photo by using camera, then I want to pass this photo to the second activity. In the second activity, I want to upload this photo to a web API, and the type of the uploaded image has to be "JPEG".
I'm not sure I should use bundle or not. Or should I use "URI"? But when I call this:
URI photo_uri = photo.toURI();
There would be some problem. I couldn't run it. How can I fix this? This is what I wrote now, I use bundle:
// photo -> photo_uri, "photo.toURI()" doesn't work
Intent intent = new Intent(this, Main4Activity.class);
intent.putExtra("URI", photo_uri);
startActivity(intent);
And how can I receive the data(in JPEG type) in another activity?
You can send the file or uri path to second activity. After getting file or uri path you can convert that path into the file and upload it on the server.
You should check the Android docs about using the camera, there you have how to get the Uri, you must NEVER try to share the Bitmap itself.
About passing data between activities, you should use the Bundle so check this doc about Parcelables and Bundles which is the way to share data.
Regards!

Displaying Images in Gallery and sharing them

My application is capable of capturing images and saving them (in the public Pictures directory, retrieved by Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).
Now, I want to be able to display those images in the system's gallery application. In order to achieve that, I create an Intent like this (filePath is a String containing the image's path):
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(filePath));
viewIntent.setDataAndType(uri, "image/jpeg");
startActivity(Intent.createChooser(viewIntent, null));
Displaying an image this way works perfectly fine, but the gallery's Share functionality doesn't seem to work:
When I press the Share-button, I get to choose the app I want to share to; but once I select it, nothing happens, as if the target application does not receive any data.
Is there any explanation why this behaviour occurs and how it can be fixed?
Edit: I used Intent Intercept to capture the Intent created by the Gallery/Photos app:

Intent with ACTION_VIEW and uri pointing to a ParcelFileDescriptor in EXTRA_STREAM

what is the correct way how I should form the intent to show content from my app in 3rd party viewers? I need to show images in gallery (or any other image viewer), pdfs in some pdf reader,..
Data gets server through a content provider which implements the openFile() method and returns a output pipe..
ParcelFileDescriptor[] pipe=ParcelFileDescriptor.createPipe();
...
ParcelFileDescriptor.AutoCloseOutputStream stream = new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]);
PipeThread pipeThread = new PipeThread(fileContents, stream);
pipeThread.start();
return pipe[0];
For images I use this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
I'm creating then a chooser for this intent as usual that's not the issue..
My problem is that although I see for example the Photos app in the chooser, I just cannot open the file in it..It just only opens the gallery with my images.
It's working when I use the action send, apps like gmail, drive, dropbox,..all of them are able to correctly read the image from the provider.
Also Skitch seems to be the only one app which I have tested it on that is able to open the image also using the Intent.ACTION_VIEW action..
Please don't tell me I should just send the URI, I really need to provide the file as a stream, or somehow as a serie of bytes (IPC limitations would be probably against this). I can't save the file to a public directory.
So the issue was that have been setting Intent type and data in two separate method calls..
What I didn't know is that Intent.setType() clears its data and Intent.setData() clears its type..
When I set both data and type through the Intent.setDataAndType() method call, it works even for URI pointing to a stream.
Unfortunately the final implementation is still not working flawlessly everywhere.
It works in default android gallery app, in G+ Photos app, in QuickPic, in Sony gallery app, but it does not work in default HTC gallery neither in default Samsung gallery.
Its just a pity, that its actually not that much dependent on my implementation as on how is it implemented in the 3rd party viewer app.

android - how to add to intent an internel stored image?

I have mange to store on my app imges files.
the directory path that contains the images is -
/data/data/yourapp/app_data/imageDir
now what im want to do is to add the relevent image to an intent -
so i have used the next code for the text parts -
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
share.putExtra(Intent.EXTRA_SUBJECT, name);
share.putExtra(Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(share, "Share with ?"));
But i don'tunderstand how can i add an image file-
by the way i have an option to get the full path of the image, but i sure don't understand how to add it to intent - so thanks for any kind of help
Let me try it clear it a bit -
if let's say i want to add the image to an e-mail - could it be enough to add to intent the image uri on the memory?!? and if so, what should i add to tne intent than, thanks
maybe to pass the whole image,you can do it just like passing objects of classes
then you will have to use parceable and all that which will be very tedious for you,but instead of sending the whole image(which may be very big sometimes)you can just pass the url of the image and fetch the url on the other activity and operate on it.It's a more efficient way to do it....
if you really want to pass the image you can convert the image into it's byte code using bitmapdecodefactory and send it,and then convert it back again....
Unless you plan on sending a very small image (kilobytes) I would actually recommend against sending images this way. It's usually better to simply send the URI or file system path, and allow the caller to retrieve the image that way.
Place it in the "extras" or similar within the intent you plan on sending.
If you plan on using internal storage (versus external images or media paths on the SDCard) you'll most likely want to create a ContentProvider / ContentResolver that other applications can use to get those image files from your sandboxed protected storage for your app.

Android load image from url and save to phone

Currently I have the following:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(mediaLoc)), "image/" + type);
context.startActivity(intent);
This was because the app used to download images from urls and saved them into the sdcard before viewing. But now I want to change the implementation to show the image from a url in the default image viewer when ACTION_VIEW is called and give them the option to save. Is this possible with ACTION_VIEW? I'm asking because I wish to use the default image viewer in the phone as it has already handled the zoom functions.
You would have to make a custom action_view for it.

Categories

Resources