Android System app not able to create directories - android

I am developing an android app.In one of the modules I am creating a directory in the external storage to store files using the below code.Things work fine and I am able to create the directory.
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnCreateDir = (Button) findViewById(R.id.createDir);
btnCreateDir.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Toast.makeText(getApplicationContext(),"Can't create directory",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"directory created/exists",Toast.LENGTH_LONG).show();
}
} catch (Exception e4) {
e4.printStackTrace();
}
}
});
}
private File getDir() {
File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return new File(sdDir, "GaPopupFile");
}
}
However, when I make this app as a system app(by signing with the system certificates and storing in system/app folder) I see that this same code when executed,returns 'Can't create directory' . How is this possible,what am I missing here ?

Have you added this permission in AndroidManifest.xml file?
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Read permission needed to read file in Android applications.
Write permission needed to write file in Android applications.

Related

Folder created inside Internal Storage not showing up

I am trying to create a folder inside Internal Storage on Button click, but I can't figure out why it's not working.
backUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
File sub = new File(MainActivity.this.getFilesDir(), "new_folder");
if (!sub.exists()){
sub.mkdirs();
Toast.makeText(getApplicationContext(), "Sucess!!",
Toast.LENGTH_LONG).show();
}
}
});
Looks alright, displays the sucess toast but can't see the folder in directory. Even tried refreshing the Directory with:
MediaScannerConnection.scanFile(MainActivity.this, new String[] {sub.toString()}, null, null);
Also provided WRITE_INTERNAL_STORAGE permission in Menifest.
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
You will need permissions, in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
on Button click Context.MODE_PRIVATE is use
File mydir = context.getDir("users", Context.MODE_PRIVATE); //Creating an internal dir;
if (!mydir.exists())
{
mydir.mkdirs();
}

file chooser to choose folder (android)

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.

How to create directory in android framework?

I want to create my directory in android framework.
For example, "/stoage/myTestDir/"
So, i try to add this code in "Kitkat/frameworks/base/services/java/com/android/server/BootReceiver.java".
BootReceiver.java
#Overrinde
public void onReceive(final Context context, Intent intent){
/*...*/
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
File testDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/myTestDir");
if(!testDir.exists(){
if(testDir.mkdirs()==true){
Log.i("mkdirs() success", "mkdirs() success");
else{
Log.i("mkdirs() fail", "mkdirs() fail");
}
}
/*...*/
}
After framework building, log shows "mkdirs() fail".
So why cannot fail to create dir in android framework with my code?
thanks.
add permissions WRITE_EXTERNAL_STORAGE in your manifest file (http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE)
example: http://screencast.com/t/iwdt3wFk4bV

Android Delete Image from SD Card with OnClick

I'm trying to simply delete an image from a simple app. I have it so that when you click on the image, it'll bring up an a dialog with the option to delete it. I thought this would just be something simple, but everything I have been trying doesn't seem to be doing anything. Below is my code. Any ideas would be greatly appreciated.
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = viewIt.getId();
File file = new File("file://" + arrPath[id]+".jpg");
file.delete();
}
});
Have you added permission in manifest file ?
Any app that declares the WRITE_EXTERNAL_STORAGE permission is implicitly granted READ_EXTERNAL_STORAGE permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Get the path of the file required and delete the file
File file= new File(android.os.Environment.getExternalStorageDirectory()+ "/myfolder/myimage.jpg");
if(file.exists())
{
file.delete();
}

Open PDF in android app

I am working on an application where need to open the pdf file in the device,
I have actually got the code on the web that is similar to most of the examples. But, the thing is that I am not able to open the file and the control goes to the "Exception" part directly.
Here is the code below:
public class MyPDFDemo extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button OpenPDF = (Button) findViewById(R.id.button);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("/sdcard/Determine_RGB_Codes_With_Powerpoint [PDF Library].pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MyPDFDemo.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
}
});
}
When I run this code : i used to see " No Application available to view pdf ". Can anyone please me to view the pdf file.
Since your catch block has ActivityNotFoundException, it means that you don't have any activity/application that can read a 'application/pdf' filetype format. Install any pdf viewer from the Android Market (Adobe recently release theirs), or else use the above mentioned open source pdf viewer and your problem will most probably will be solved.
http://code.google.com/p/apv/downloads/list
https://market.android.com/details?id=cx.hell.android.pdfview&feature=search_result
When you start activity with your given params, it searches for all applications/Activities/Intents that are registered to open pdf format. Since you have none in your device, you are getting the ActivityNotFoundException
First install the pdf reader on device.
than use this code for reading pdf file from internal memory.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView tv = (TextView)findViewById(R.id.tv);
Button bt=(Button)findViewById(R.id.openbtn);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File pdfFile = new File(Environment.getExternalStorageDirectory(),"Leave.pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try{
startActivity(pdfIntent);
}catch(ActivityNotFoundException e){
tv.setText("No Application available to view PDF");
}
}
else
{
tv.setText("File not found");
}
}
});
}
Your code is right, I have also used same code to open pdf file within a viewer.
As you don't have a viewer installed to your device so it can't open without any viewer.
You can install Adobe reader for Android.
I can't open pdf file within emulator, so I have to test using my device.

Categories

Resources