i am using afilechooser for this purpose . and this is by default programmed to choose the items inside the folder and get you the path that is selected by the user.
but i want to use this as folder chooser where the user choose a location from internal memory of android device and then the app will save the file at that location.
so how do i do it.
the code i am using for this purpose is-
private void showChooser() {
// Use the GET_CONTENT intent from the utility class
Intent target = FileUtils.createGetContentIntent();
// Create the chooser Intent
Intent intent = Intent.createChooser(
target, getString(R.string.chooser_title));
try {
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// The reason for the existence of aFileChooser
}
}
and i suspect the code can be changed to choose the folder instead of files. any suggestion can be helpful . please suggest if any other way to achieve what is want .
thank you
Looking at the github project in the url you posted, it doesn't look like this can be achieved. My claim is based on the following piece of code inside com.ipaulpro.afilechooser.FileChooserActivity class:
#Override
public void onFileSelected(File file) {
if (file != null) {
if (file.isDirectory()) {
replaceFragment(file);
} else {
finishWithResult(file);
}
} else {
Toast.makeText(FileChooserActivity.this, R.string.error_selecting_file,
Toast.LENGTH_SHORT).show();
}
}
just look at the if(file.isDirectory()) statement.
Related
I am working on an android app that is supposed to have copy of deleted image from android gallery. If the user wants to delete a picture from gallery, before he/she delete it, our app must save a copy of the image in a hidden folder.
I used SMS receiver but this one is some thing confusing.
public MyReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
//activity which I want to perform here is to have a copy of deleted image in a hidden folder
}
You canot track a file when it is deleted. You can use file observer for track other cahnges in a folder.
observer = new FileObserver(Your_folder_Path) { // set up a file observer to watch this directory on sd card
#Override
public void onEvent(int event, String file) {
//if(event == FileObserver.CREATE && !file.equals(".probe")){ // check if its a "create" and not equal to .probe because thats created every time camera is launched
Log.d(TAG, "File created [" + pathToWatch + file + "]");
Toast.makeText(getBaseContext(), file + " was saved!", Toast.LENGTH_LONG).show();
//}
}
};
observer.startWatching();
Hi I am fairly an amateur at android so I might not be realizing something obvious.
I have a method that populates a global File Array variable with a list of flies in a specific directory. Problem is everything works fine if the directory has been made before by using my app to save a file there however when the user hasn't done that an error message is suppose to pop up saying they haven't saved a file yet.
I do a check if the directory exist but the app crashes when the directory has not been created.
This is what my code looks like any assistance would be appreciated
private void getTemplates()
{
//Gets file directory for saved templates
File finalMarkTemplateDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Final Mark Templates");
//Checks if path exists in other word if any templates have been saved before
if(finalMarkTemplateDir.exists())
{
templatePaths = finalMarkTemplateDir.listFiles();
}
else
{
Toast.makeText(this, "No previous templates have been saved.", Toast.LENGTH_LONG).show();
setResult(RESULT_CANCELED);
finish();
}
}
I am too an amateur, you have not created a file in your code, calling a new file() method does not create a file. Pls check that out
try {
finalMarkTemplateDir.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I managed to solve my problem when I call the setResult and finish methods I did not realize the flow of the program is returned to my onCreate method which meant the rest of my method calls in onCreate was still being called and they require the templatePaths array.
So basically I thought finish would stop the processing and move back to the calling class(using startActivityForResult). Instead I now call finish from my onCreate and use a boolean to determine if I could successfully access the directory.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.dialog_load_template);
boolean fileLoadStatus = getTemplates();
if(fileLoadStatus)
{
populateTemplateList(templatePaths);
}
else
{
setResult(RESULT_CANCELED);
finish();
}
}
private boolean getTemplates()
{
boolean fileLoadStatus = false;
//Gets file directory for saved templates
File finalMarkTemplateDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Final Mark Templates");
//Checks if path exists in other word if any templates have been saved before
if(finalMarkTemplateDir.isDirectory())
{
templatePaths = finalMarkTemplateDir.listFiles();
fileLoadStatus = true;
}
else
{
Toast.makeText(this, "No previous templates have been saved.", Toast.LENGTH_LONG).show();
}
return fileLoadStatus;
}
I'm attempting to use some code to load a basic changelog from a text file into an alert dialog. The only issue is that I am having trouble getting my code to find the location of my changelog file. The file "changelog" is located in app/src/main/. Here is a sample of my code.
private void displayChangelogDialog() {
Context context = this;
try {
new AlertDialog.Builder(this)
.setTitle("Changelog")
.setMessage(readFile(getApplicationContext().getFilesDir().getAbsolutePath() + "/app/src/main/changelog"))
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
} catch (IOException e) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
I am not very familiar with getFilesDir() or getAbsolutePath(), so I'd imagine this would be a good learning experience if anyone can help me figure out what I'm doing wrong. I'm sure the problem resides in the path I have entered, but I am a bit stumped at the moment. Thanks in advance.
getFilesDir documentation:
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int) are stored.
Typically, openFileOutput access to /data/data, which is completly different from your application install path.
If you want to access a file, put it in the assets folder the check the assets related functions (see AssetManaget)
If I create an app that depends on another app or apps (eg: the Facebook and Twitter apps), yet they are not installed, is there a method of checking for those dependencies and installing them at the same time as my own app?
I did this in my application which requires the zxing scanner app to be installed.
You will want this inside your onclick or ontouch:
try{
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.setPackage("com.google.zxing.client.android");
startActivityForResult(intent, 0);
} catch (Exception e) {
createAlert("Barcode Scanner not installed!", "This application uses " +
"the open source barcode scanner by ZXing Team, you need to install " +
"this before you can use this software!", true);
}
which calls
public void createAlert(String title, String message, Boolean button) {
// http://androidideasblog.blogspot.com/2010/02/how-to-add-messagebox-in-android.html
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
if ((button == true)) {
alertDialog.setButton("Download Now",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent browserIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("market://search?q=pname:com.google.zxing.client.android"));
startActivity(browserIntent);
}
});
}
alertDialog.show();
}
Then after sorting out all that code out I realise you asked for it to be installed at the same time as your app. Not sure if i should post this code, but it may be helpful
Short answer: No, you cannot automatically install other applications as dependencies.
Longer answer:
Android Market does not let you declare other applications to install as a dependency. As a system, Market appears to be designed for single application installs -- not Linux distro style mega dependency graphs.
At runtime, you can test for installed apps and punt your user over to the Market if so. See the techniques suggested by #QuickNick (testing if an app is installed) and #TerryProbert (punting to market) if that's what you want.
Your best bet is probably to design your app to gracefully degrade if dependencies are not available, and suggest (or insist) that they head over to market to install them.
Start from this:
Intent mediaIntent = new Intent("com.example.intent.action.NAME");
// add needed categories
List<ResolveInfo> listResolveInfo = getPackageManager().queryIntentServices(mediaIntent, 0);
if (listResolveInfo.size() != 0) {
//normal behavior
} else {
//install what you need
}
I give you example of querying services. If you want to check activities, then you will call queryIntentActivities().
I think following the pattern outlined in this post on the Android Developer Blog will help you.
http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html
As TerryProbert points out if you know that the Intent is not available prompt the user to install the missing app.
Here's what I use to return the first mission activity that exists:
try {
Class<?> missionClass = Class.forName(mPackageName+".Mission"+mission);
Method missionDescription;
missionDescription = missionClass.getMethod("missionDescription");
mMissionDescription = (String) missionDescription.invoke(null);
if (mMissionDescription.length() > 0) {
nextMission = mission;
break;
}
} catch (Exception e) {
//DEBUG*/Log.v(this.getClass().getName(), "onResume: Mission no "+mission+" not found: "+e.getMessage());
}
Each mission is held in a separate class, derived from a Mission base class. Derived classes are called Mission1, Mission24 etc.
Not all missions are defined.
The base class has an abstract class missionDescription which returns a string describing the mission.
This code is inside a loop so tests mission=1 to 99, trying to call missionDescription. It returns when the Description for the first mission found is returned.
The PDF content can be of online as well as offline mode, I need to show pdf in my own customized layout so, Intent can't be used. Any suggestion will be appreciated. thank you.
Yes we are able to show pdf content onLine with the help of google doc api Here i am given code for using it. for ON Line Mode
public class ReaderActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView=(WebView)this.findViewById(R.id.WebView01);
Intent intent = new Intent(Intent.ACTION_VIEW);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setPluginsEnabled(true);
webView.loadUrl("https://docs.google.com/viewer?url=http%3A%2F%2Fwww.eli.sdsu.edu%2Fcourses%2Ffall09%2Fcs696%2Fnotes%2FAndroid13Web.pdf");
//intent.setDataAndType(Uri.parse("https://docs.google.com/viewer?url=---------Your URL");
}}
for OFF Line Mode
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();
}
Note-:In off line mode first you have download file form server to own local device in sd-card the get path of this document and put it on in the place of path variable then you get answer of your hole question.
In this code you should use your url in the place my url.
I hope this is help.
First of there is no support for pdf in Android so you need to open in some other app like adob or if you want to do it right way then need make the load lib like vudroid and apdfviewer .
apdfviewer is very good but there is no support how to compile source code, actually all lib work with c++ in backend so you need to install the ndk.
Vudroid is slow but you can compile easily.
I hope this will help you.
But
Some phones (like the Nexus One) come with a version of Quickoffice pre-installed so it may be as easy as sending the appropriate Intent once you've saved the file to the SD card.
public class OpenPdf extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
I found that Internet connectivity mode :- through google doc we can open the pdf.
but in the Internet offline mode:- we need to use Intent and open the pdf file through any one of the pdf viewer application install in the device.
one things we can do in the offline mode is that, instead of opening the option of multiple pdf viewer reader in device and select one application to open pdf file we can open the pdf with the particular application.