Retrieve filename from a web folder - android

I am using AsyncTask to download a file from a web folder. However, the name of the file will be different every time.
Is there a way to get the name of the file in a web folder? (There will be only 1 file in that folder at a given time).
http://www.example.com/myfolder/myfile_1

Try the ApacheURLLister (I didn't tested it but it may work):
URL url;
List serverDir = null;
try {
url = new URL("http://www.abc.com/myfolder/");
ApacheURLLister lister = new ApacheURLLister();
serverDir = lister.listAll(url);
}
catch (Exception e) {
e.printStackTrace();
Log.e("ERROR ON GETTING FILE","Error is " +e);
}
System.out.println(serverDir);
return serverDir;

Related

create an inner custom folder with Environment.getExternalStoragePublicDirectory()

i am using this following code to save a created pdf inside my phone;
File file = new File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),"/" +user.getInjurdManFirstName() +user.getInjuredManSecondName()+ "-" + user.getIdNumber()+".pdf");
try{
myPdfdocument.writeTo(new FileOutputStream(file));
showMessage("SUCCES!");
}catch (IOException e){
e.printStackTrace();
showMessage("NO SUCCES");
}
the code works good but it save me the pdf file in the download folder, i want it to save it like: DOWNLOADS/"custom folder/"name.pdf"
i tried to use Context and its doesnt work

How should I save a json into my external storage?

I implemented a share button in my app. When I want to share, I can select a saved json data from the device and select via which way I want to share it (mail etc.). The problem is, that the data is NOT in the attachements. The problem is likely because I use the internal app storage. Therefore I want to save tje json data into the external storage, what would be better in my case anyway. But I am not really sure how to do that. I am not sure if I should use the Media type of content of the Documents and other files type of content which is provided by android. There is also the Appspecific files type but this looks like it is not applicaple for me, because I need to share the json data wit ha share functin. At the moment my code looks like this:
Save Function, which get's a file name I can choose myself
private void saveState(String name) {
File file = new File(getFilesDir(), name + ".json");
try{
OutputStream out = new FileOutputStream(file);
MyJsonWriter writer = new MyJsonWriter();
writer.writeJsonStream(out, ... //data structure);
out.close();
}catch (Exception e){
Log.e("saveState ERROR", "----------------------------------------------------");
}
}
LoadButtonClick Functin which shows me all files
public void loadStateClick(View view) {
final LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
String[] files = MainActivity.this.fileList();
... //more code which is not important here
Load Function
private void loadState(String name) {
File file = new File(getFilesDir(), name);
InputStream in = null;
... //setting my data structure, not important here
try{
in = new FileInputStream(file);
MyJsonReader reader = new MyJsonReader(MainActivity.this);
SaveData savedData = reader.readJsonStream(in);
... // handling data structure, not important here
in.close();
}catch (Exception e){
Log.e("LOAD ERROR", e.toString());
}
}

openCSV can not find the saved file

I´m trying to save my values to .csv file with this code.
try {
CSVWriter writer = new CSVWriter(new FileWriter(getFilesDir()+"ECGValues.csv"),',');
// feed in your array (or convert your data to an array)
String[] entries = Values_to_save.toArray(new String[Values_to_save.size()]);
writer.writeNext(entries);
writer.close();
Values_to_save.clear();
Toast.makeText(this,"ECG values saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this,"Error while saving values", Toast.LENGTH_SHORT).show();
Log.e("error", "" + e.getMessage());
Log.e("error",""+ e.getStackTrace());
}
My input data is from
Lead_I_save =String.valueOf(IValue);
Lead_II_save = String.valueOf(IIValue);
Lead_III_save = String.valueOf(IIIValue);
Lead_aVL_save = String.valueOf(aVLValue);
Lead_aVF_save = String.valueOf(aVFValue);
Lead_aVR_save = String.valueOf(aVRValue);
String newLine = String.format("%1$s;%2$s;%3$s;%4$s;%5$s;%6$s;",
Lead_I_save, Lead_II_save, Lead_III_save,
Lead_aVL_save, Lead_aVF_save, Lead_aVR_save);
Values_to_save.add(newLine);
A Toast appears with ECG values saved, but I cannot find the .csv anywhere.
I will welcome any suggestions. Thanks
You used a relative path (only a file name) and then your file lands in the apps private internal memory which is unreachable for other apps. Use a full path to external memory instead.

ESelect and display png from custom folder

I've created a folder 'test' inside res and I want to display them in an image view. How exactly can I fetch the image with a given name in the designated folder?
ImageView test = (ImageView) testing.findViewById(R.id.test);
flag.setImageDrawable(getResources().); <==== This?
EDIT
InputStream is = null;
try {
is = this.getResources().getAssets().open("country_flags/sample.png");
} catch (IOException e) {
;
}
image = (ImageDrawable) BitmapFactory.decodeStream(is);
try {
flag.setImageDrawable(getResources().getAssets().open("country_flags/"+nationality+".png"));
} catch (IOException e) {
e.printStackTrace();
}
How exactly can I fetch the image with a given name in the designated folder?
You don't. You cannot invent new resource types, and so your test directory will, at best, be forever ignored.

How to read a file directly from a .zip file without extracting it in android

I have been working on android for the past few months, now the problem for me is to read a .zip file placed on sdcard. I have successfully done the coding for downloading the .zip file on the sdcard.
I have img.zip file downloaded on to the sdcard. This img.zip contains 5 image files. Now instead of unzipping the img.zip can i directly read its content...??? if yes plz help. I saw few examples over internet but they all say to unzip and then use, i want to avoid that part because i simply want to set the images for an imageview.
ImageView imv = new ImageView(this);
imv.setImageURI(Uri.parse("//sdcard/1.png"));
this is like downloading a single image and setting the source of imv which actually works. Now what i want is something as shown below.
imv.setImageURI(Uri.parse("//sdcard/img.zip/1.png"));
I tried this, but in my layout i don't see the images.
can it be done... plz help...
I got it working by the following code....
try {
Bitmap mBackground=null;
FileInputStream fis = new FileInputStream("//sdcard/tp.zip");
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null) {
if (ze.getName().equals("1.png")) {
Toast.makeText(con, "Found", 2).show();
mBackground = BitmapFactory.decodeStream(zis);
imv.setImageBitmap(mBackground);
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Try
imv.setImageURI(Uri.parse("//sdcard/img.zip!/1.png"));

Categories

Resources