Open file picker interface programatically on xamarin android - android

Am trying to get my app to display the file picker interface programatically on button click but when i click on that button, my app crashes...
The button has the label "open" and its supposed to filter files and display only files with the extension ".txt",Here is the code of the method that am trying to achieve that with...
private void Button2_Click(object sender, EventArgs e)
{
//Program this button to open a file picker interface
Intent intent = new Intent(BaseContext, FilesDir.Class);
intent.PutExtra(FilesDir.AbsolutePath, true);
StartActivityForResult(intent,1);
}
I used java code as blueprint to achieve this but it seems there are some huge difference lines between these two approaches of android programming, Your help is greatly appreciated....
Additional code to help the intent filter for text files only is also greatly welcome too

Have a try with codes below:
Intent intent = new Intent();
intent.SetType("text/plain");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select file"), 1);
If you have java code, just update them in the question and I can help you to translate them to C#.
Refer: how-to-open-pdf-or-txt-file-in-default-app-on-xamarin-forms

Related

Android Document Picker like whatsapp

I have been able to open audio file picker like whatsapp using following code.
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
return intent;
However, I want to open the file picker in the same as it is shown in whatsapp. I am using the following code and other similar codes. However, I am not able to show document picker exact like whatsapp. The following code opens the document picker and it shows explorer and take me to Recent folder as starting point.
final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// The MIME data type filter
intent.setType("application/*");
// Only return URIs that can be opened with ContentResolver
intent.addCategory(Intent.CATEGORY_OPENABLE);
It shows differently on different devices as well. I want to make it consistent.
Apologies for giving answer late. I don't know this is exact answer you are looking for. But i implemented image and document picker with below code.
https://github.com/DroidNinja/Android-FilePicker

Why doesn't my App Chooser dialogue show?

I am using this tutorial to learn about the basics of Intents in Android. The code sample I am using is adopted from the section named "Force an App Chooser" on the page linked.
The following is the method which should be invoked on the click of the button.
public void startSecondActivity(View view) {
Intent intent = new Intent(Intent.ACTION_SEND);
String fileChooserLabel = getResources().getString(R.string.fileChooserLabel);
Intent fileChooserIntent = Intent.createChooser(intent, fileChooserLabel);
if (intent.resolveActivity(getPackageManager())!=null) {
startActivity(intent);
} else {
textView = (TextView) findViewById(R.id.text_view);
textView.setText("False");
}
}
But it just enters the else block of the if-else conditional. I tried this app on both a real device and the emulator. So can anybody point out what might be wrong here, and what I can do about it.
Note: I did not add anything to the Manifest file, since I am using Eclipse IDE and I suppose whatever is required at this point is automatically added to the manifest file.
It is returning null because there are no activities on the device that support your Intent. In this case, your ACTION_SEND Intent is not properly set up.
Note that the code sample you used as a reference is not a tutorial. It is not designed to be a complete code sample. In fact, what they list there will not even compile, as their ... is meant to be replaced by your own code to complete setting up the Intent.
You will need to fully configure your ACTION_SEND Intent, most notably setting the MIME type, as is covered elsewhere in the documentation. Replacing:
Intent intent = new Intent(Intent.ACTION_SEND);
with something like:
Intent intent = new Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, "IM IN UR STAK OVERFLO ANZWR");
should suffice.

File dialogue replacement?

I have a small app where some people can have a 1:1 chat with each other. Now I want to implement a possibility to share some files (images, sounds, whatever,...).
My problem: I need a possibility to let the user choose such a file. Since I did not find some kind of "file dialogue" for Android: what is the best way to do this?
I do not need a ready-to use solution but just some hints/ideas how this could be done out of an app.
Thanks!
You can have one Button in your screen and clicking on that button let user choose a file from gallary using below code,
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);

PDF rendering in android

I am newbie to android and development world. I need help to solve this problem.(scenario as follows)
I have created one android app using HTML5 and CSS and bundle it out using phonegap.
Now I need to display PDF file in this app with two options for user whether first download and then read or second read online
So My problem is, I am not able to dispaly PDF in app.
How could I achieve this? please help . I am trying to solve it from last 4 days and tried out each and solution which is already given in forum , but still no success.
It would be great for me if someone ans step by step procedure or one example to refer.
Thank You
Minal
This may help: How to open a PDF via Intent from SD card
The basic idea is to get a pdf app to render the pdf for you, and your app just kicks off an intent to do that.
try this code
private void openBook() {
File file = new File(mRealPath);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, getString(R.string.application_type));
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(FirstTab.this,
getString(R.string.no_application_found),
Toast.LENGTH_SHORT).show();
}
}
you can open pdf file via intent like this
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),”application/pdf”);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

Open device gallary for view only

I find this code here to open the gallery from my own btn:
btnGallery.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0);
}
});
It works, but when I click on a photo it exit the viewer.
I guess it because the "createChooser".
How can I change it to only view the photos and not to choose them?
As far as I know this is impossible.
If you had access to a full class name of an activity to start the Gallery app you could call it as you usually do it Activity.startActivity(Context context, Class clazz). But Gallery classes is an internal API which you have no direct access to.
Well, Gallery app is accessible via throwing an appropriate Intent like the one in your sample code. By setting action name intent.setAction(Intent.ACTION_GET_CONTENT) you request the behavior you've got (browse all images, select one, get back to the caller activity with a uri of a selected image). There is also another possible action Intent.ACTION_VIEW, and if set with a uri of an image it causes Gallery to show that image for you. But that's all we can request from Gallery (there are no other predefined actions to suit your needs - just to browse images).
So, a way out is to create your own custom image browser activity.

Categories

Resources