Passing data through intent for onActivityResult - android

Im trying to save a pdf file on a user-picked location but Im having problems passing data through the intent.
I tried using a bundle to pass the info but it is always null.
It only works if I add a local variable and assign it in the #save method. But I want to pass it through the intent.
The thing is im not going into another activity, just to choose a directory and im coming back.
`protected void save(byte[] bytes, String fileName, String mimeType) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.setType(mimeType);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_TITLE, fileName);
intent.putExtra("bytes", bytes);
activityResultLaunch.launch(intent);
}`
`ActivityResultLauncher<Intent> activityResultLaunch = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
Intent data = result.getData();
try {
Uri uri = data.getData();
OutputStream outputStream = getContext().getContentResolver().openOutputStream(uri);
// here it is always null
//byte[] bytes = data.getByteArrayExtra("bytes");
outputStream.write(bytes);
outputStream.close();
Toast.makeText(getContext(), "Successfully saved document to device!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getContext(), "Failed to save document to device.", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});`
Any suggestions? I dont see what im missing here, ive been banging my head on this for a couple of hours and couldnt get it to work. Any help is greatly appreciated!

intent.putExtra("bytes", bytes);
Here, you are passing an Intent extra to the activity that you are starting. That activity is a device-supplied activity that responds to ACTION_CREATE_DOCUMENT.
More importantly, it is not your activity. It is certainly not the activity that is calling launch() for that Intent.
So, that bytes extra will be available to the ACTION_OPEN_DOCUMENT activity. That activity will ignore that extra. It will not include that extra in the completely different Intent that is delivered to your onActivityResult() method.
But I want to pass it through the intent.
That is not an option in Android, sorry.

Related

passing high resolution images from one activity to other

I'm creating an image filter app in Android studio. first, the user selects an image from gallery and it will be displayed in imageview. Then the user clicks edit button and that image is displayed in imageview of next activity where we can add filters... It works fine with low resolution images but when I select any high resolution image it is shown in first imageview but when I click edit button either the app crashes or the last image I had selected is displayed.I searched for the solution but couldn't find it. If anyone knows how to solve this problem please help me
There is a limit to the size of data that can be passed through an intent. The limit is roughly 500Kb - your high resolution photographs will be larger than this.
Consider saving your image to a file location on the device, passing the URI to the receiving activity and loading it within there.
first paste crash logs.
then instead of passing image itself just pass image path.
or simply add the edit tools and mainView in one activity and make edit tools invisible! however you can use fragment too.
use with putExtra to send the Uri Path:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent .setClass(ThisActivity.this, NewActivity.class);
intent .putExtra("KEY", Uri);
startActivity(intent );
You just need to add path of image.
It's better to save the image in storage and pass the Uri of location instead of passing the image.
Save image in storage:-
public static Uri saveImageOnExternalStorage(Bitmap capturedBitmap, String imageId) {
if (null != capturedBitmap ) {
OutputStream fOutputStream;
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path + "temp", mediaId + ".png");
file.delete();
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
if (file.createNewFile()) {
fOutputStream = new FileOutputStream(file);
capturedBitmap.compress(COMPRESS_FORMAT, 100, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
return Uri.fromFile(file); // return saved image path on external storage
}
} catch (FileNotFoundException e) {
Log.e(TAG,e.getMessage());
return null;
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG,e.getMessage());
}
}
return null;
}
Now the same Uri you can pass in the intent of next activity:-
Intent intent = new Intent(CurrentActivity.this, LaunchActivity.class);
intent .putExtra("image_key", Uri);
startActivity(intent );

How to get Itent.ACTION_SEND working for any file type

I use the following snippet to open or share any file from device's storage (MyFile is my own class which extends File and should be considered as File. The flag String I'm passing is either Intent.ACTION_VIEW or Intent_ACTION_SEND):
public void openOrShare(String flag, MyFile f){
try {
MimeTypeMap mmap = MimeTypeMap.getSingleton();
String type = MimeTypeMap.getFileExtensionFromUrl(f
.getName());
String ftype = mmap.getMimeTypeFromExtension(type);
if (ftype == null)
ftype = "*/*";
Intent intent = new Intent(flag);
Uri data = Uri.fromFile(f);
intent.setDataAndType(data, ftype);
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Tools.gimmeToast(
getActivity(),
"no application found to handle this file type",
Toast.LENGTH_LONG);
} catch (Exception e) {
e.printStackTrace();
}
}
While passing Intent.ACTION_VIEW everything works fine for any (also custom) types, the system creates a chooser and lists the apps, for well-known files it launches the correct Activity to handle the file immediately .
The problem: passing Intent.ACTION_SEND seems to be working halfway - it creates the chooser as well, however most apps (Dropbox and many more that I've tested with) just crash with an NPE when confirming the action. Testing with various email clients also failed: they don't crash like most of the other apps, but create a message and put the local Uri (like //storage/.....) in the To field (gmail) OR simply create a new empty message, ignoring the Intent data (yahoo! mail), while I expect them to attach the file to a new message.
The question: what am I doing wrong to share any file type?
EDIT
I figured out that it works if using intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));, however there's an ActivityNotFoundException being caught when trying to share some specific files (like .dat). As far as I know, apps like Dropbox support adding any file types. Looking for a workaround.
If you don't know the MIME Type of the file, then do not set it.
So I would try:
Intent intent = new Intent(flag);
Uri data = Uri.fromFile(f);
String ftype = mmap.getMimeTypeFromExtension(type);
if (ftype != null) {
intent.setDataAndType(data, ftype);
} else {
intent.setData(data);
}
setDataAndType needs data and explicit mime type (maybe */* is not).
Never mind, I finally figured that out. So here's the working solution which allows to share any type of data using any app*:
*Note: the below code actually also lists apps that might be not able to handle specific file types, those apps (at least they should) notify the user that the file type is not supported if this is the case
public void doShare(MyFile f) {
try {
Intent intent = new Intent(Intent.ACTION_SEND);
MimeTypeMap mmap = MimeTypeMap.getSingleton();
String type = MimeTypeMap.getFileExtensionFromUrl(f.getName());
String ftype = mmap.getMimeTypeFromExtension(type);
if (ftype == null)
ftype = "*/*";
intent.setType(ftype);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Tools.gimmeToast(getActivity(),
"no application found to handle this file type",
Toast.LENGTH_LONG);
} catch (Exception e) {
e.printStackTrace();
}
}

How to open file save dialog in android?

I have a web service which give me a byte[] array according to image id . I want to convert these byte[] to file and store a file on android where user want like save file dialog box with file same format exactly it has.
Since this is the top result in google when you search for that topic and it confused me a lot when I researched it, I thought I add an update to this question.
Since Android 19 there IS a built in save dialog. You dont event need any permission to do it (not even WRITE_EXTERNAL_STORAGE).
The way it works is pretty simple:
//send an ACTION_CREATE_DOCUMENT intent to the system. It will open a dialog where the user can choose a location and a filename
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("YOUR FILETYPE"); //not needed, but maybe usefull
intent.putExtra(Intent.EXTRA_TITLE, "YOUR FILENAME"); //not needed, but maybe usefull
startActivityForResult(intent, SOME_INTEGER);
...
//after the user has selected a location you get an uri where you can write your data to:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == SOME_INTEGER && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
//just as an example, I am writing a String to the Uri I received from the user:
try {
OutputStream output = getContext().getContentResolver().openOutputStream(uri);
output.write(SOME_CONTENT.getBytes());
output.flush();
output.close();
}
catch(IOException e) {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
}
}
}
More here:
https://developer.android.com/guide/topics/providers/document-provider
The Android SDK does not provide its own file dialog, therefore you have to build your own.
You cant create a save file dialog but you can save files from ur application to android sd card with the help of below links
http://android-er.blogspot.com/2010/07/save-file-to-sd-card.html
http://www.blackmoonit.com/android/filebrowser/intents#intent.pick_file.new
First, you should create a dialog intent for saving the file, After selection by the user, you can write on that directory and specified the file without any read/write permissions. ( Since Android 19 )
Source:https://developer.android.com/training/data-storage/shared/documents-files#create-file
// Request code for creating a PDF document.
private final int SAVE_DOCUMENT_REQUEST_CODE = 0x445;
private File targetFile;
private void createFile() {
Uri reportFileUri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", targetFile);
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_TITLE, targetFile.getName());
// Optionally, specify a URI for the directory that should be opened in
// the system file picker when your app creates the document.
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
startActivityForResult(intent, SAVE_DOCUMENT_REQUEST_CODE );
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SAVE_DOCUMENT_REQUEST_CODE && resultCode == RESULT_OK){
Uri uri = data.getData();
saveFile(uri);
}
}
private void saveFile(Uri uri) {
try {
OutputStream output = getContentResolver().openOutputStream(uri);
FileInputStream fileInputStream = new FileInputStream(targetFile);
byte[] bytes = new byte[(int) targetFile.length()];
fileInputStream.read(bytes, 0, bytes.length);
output.write(bytes);
output.flush();
output.close();
Log.i(TAG, "done");
} catch (IOException e) {
Log.e(TAG, "onActivityResult: ", e);
}
}
#JodliDev already provided the accepted answer, however, startActivityForResult is now deprecated, so I want to provide my solution here using registerForActivityResult(ActivityResultContracts.CreateDocument())
First register a ActivityResultLauncher where you define what should happen with the result. We'll get the uri back that we can use for our OutpuStream. But make sure to initialize it at the beginning, otherwise you will get:
Fragments must call registerForActivityResult() before they are created (i.e. initialization, onAttach(), or onCreate()).
private var ics: String? = null
private val getFileUriForSavingICS = registerForActivityResult(ActivityResultContracts.CreateDocument()) { uri ->
if(ics.isNullOrEmpty())
return#registerForActivityResult
try {
val output: OutputStream? =
context?.contentResolver?.openOutputStream(uri)
output?.write(ics?.toByteArray())
output?.flush()
output?.close()
} catch (e: IOException) {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show()
}
}
Then just call your ActivityResultLauncher with .launch(...) wherever it is needed.
getFileUriForSavingICS.launch("filename.txt")
And that's about it ;-)
You can also have a closer look at ActivityResultContracts.CreateDocument(). This method provides the document saving dialog, but there are other helpful functions inside (like for starting a camera intent). Check out:
https://developer.android.com/reference/androidx/activity/result/contract/ActivityResultContracts
for the possible ActivityResultContracts
Or https://developer.android.com/training/basics/intents/result for some more training material and also some information how a custom contract could be created!

Invoking Adobe Reader from within my Android application

I am writing an Android application to display pdf files on the device. And I need to use the current versioncode (35498) of the Adobe Reader to display the pdf files.I have with code to display list of files on the screen. Now I need to invoke the Adobe reader (not any other pdf reader installed on the device) onclick of each document. I am not sure how I code that. I am an Android newbie. Any help will be greatly appreciated.
Thanks in Advance,
Navin
I see that you want to open Adobe specifically, but you may want to consider doing it the more Android-like way of opening a general intent and allowing the user to choose how it opens. For your reference, you'd do that with the following code:
private void openFile(File f, String mimeType)
{
Intent viewIntent = new Intent();
viewIntent.setAction(Intent.ACTION_VIEW);
viewIntent.setDataAndType(Uri.fromFile(file), mimeType);
// using the packagemanager to query is faster than trying startActivity
// and catching the activity not found exception, which causes a stack unwind.
List<ResolveInfo> resolved = getPackageManager().queryIntentActivities(viewIntent, 0);
if(resolved != null && resolved.size() > 0)
{
startActivity(viewIntent);
}
else
{
// notify the user they can't open it.
}
}
If you really need to use both Abode Reader specifically, and a specific version, you would need to query for it using PackageManager.getPackageInfo(String, int)
Try the following code
private void loadDocInReader(String doc)
throws ActivityNotFoundException, Exception {
try {
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.parse(doc), "application/pdf");
startActivity(intent);
} catch (ActivityNotFoundException activityNotFoundException) {
activityNotFoundException.printStackTrace();
throw activityNotFoundException;
} catch (Exception otherException) {
otherException.printStackTrace();
throw otherException;
}
}
If you are in "online mode", here is an interesting alternate solution using Google docs.
String myPDFURL = "http://{link of your pdf file}";
String link;
try {
link = "http://docs.google.com/viewer?url="
+ URLEncoder.encode(myPDFURL, "UTF-8")
+ "&embedded=true";
} catch (Exception e) {
e.printStackTrace();
}
Uri uri = Uri.parse(link);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
This works, setDataAndType method cannot seem to correctly recognize the PDF type if used via URL.
private static Intent newPDFLinkIntent(String url) {
Uri pdfURL = Uri.parse(url);
Intent pdfDownloadIntent = new Intent(Intent.ACTION_VIEW, pdfURL);
pdfDownloadIntent.setType("application/pdf");
pdfDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
return pdfDownloadIntent ;
}
Unfortunately, the PDF applications I'm using don't anticipate downloading and caching the online content (some will have memory leak error, some will reject link downloading), so you'll eventually end up invoking an intent that downloads the PDF first, before opening the downloaded content via the notification link. I eventually used the solution below:
private static Intent newPDFLinkIntent(String url) {
Intent pdfDownloadIntent = null;
try {
pdfDownloadIntent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
pdfDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
} catch (URISyntaxException e) {
Log.e("PDF Link Tag", e.getMessage());
}
return pdfDownloadIntent;
}

How do I save data from Camera to disk using MediaStore on Android?

For my application, I'd been using my own Camera class for taking images and my own database but soon enough I couldn't really keep up with changes and I decided to use the built in camera application in Android to do the job, but I can't seem to get it to save file. What am I missing here? The application seems to save the file but it's just 0 bytes. I looked up the source code of the Camera application and it's looking for the "output" in Extras to save the file. Any help would be greatly appreciated.
Public class CameraTest extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button cameraButton = (Button) findViewById(R.id.cameraButton);
cameraButton.setOnClickListener( new OnClickListener(){
public void onClick(View v ){
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.BUCKET_ID, "test");
values.put(Images.Media.DESCRIPTION, "test Image taken");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra("output", uri.getPath());
startActivityForResult(intent,0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode== 0 && resultCode == Activity.RESULT_OK){
((ImageView)findViewById(R.id.pictureView)).setImageURI(data.getData());
}
}
}
This worked with the following code, granted I was being a little dumb with the last one. I still think there's got to be a better way so that the original image is still saved somewhere. It still sends me the smaller 25% size image.
public class CameraTest extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button cameraButton = (Button) findViewById(R.id.cameraButton);
cameraButton.setOnClickListener( new OnClickListener(){
public void onClick(View v ){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode== 0 && resultCode == Activity.RESULT_OK) {
Bitmap x = (Bitmap) data.getExtras().get("data");
((ImageView)findViewById(R.id.pictureView)).setImageBitmap(x);
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.BUCKET_ID, "test");
values.put(Images.Media.DESCRIPTION, "test Image taken");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
x.compress(Bitmap.CompressFormat.JPEG, 70, outstream);
outstream.close();
} catch (FileNotFoundException e) {
//
} catch (IOException e) {
//
}
}
}
Also, I do know that the cupcake release of Android should fix the small image size soon.
The below code will start the default camera and have the camera save the image to the specified uri. The key is to put the extra "MediaStore.EXTRA_OUTPUT" along with the desired uri.
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/Images/" + image_name + ".jpg");
Uri imageUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 0);
In your first attempt, it could be that the Camera app crashes when it reads your "output" extra, since it expects it to be a Uri, while you provide a String. The Camera app seems to read the extra flag after capturing the photo. You should just provide uri, and not uri.getPath(). Then, since you already know the URI to the photo, it will not be returned in the onResult call. You need to remember the URI in a member variable.
In the second attempt you will get a scaled down (50%) bitmap back. It is primarily intended for views. I think the full sized bitmap is too large for the memory budget of the application. This may be the reason for the downscale.
FYI , found this on docs :
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.
located here :http://developer.android.com/reference/android/provider/MediaStore.html
so the app can save full size image for you , if you tell it where.
**Edit : This is not the case with HTC devices. HTC (not nexus) that uses htc sense ui have branched from android 1.5 and carry a bug that always save the image in low res. you can lunch activity for camera and use the share function from camera to use the full sized image.
I got the same Problem with the Emulator,
I tried it on a real Phone an it worked,
maybe it is because the Virtual Phone can't really take Picures.

Categories

Resources