Wednesday, March 21, 2012

Splitting a URL/ Getting the file name from URL string

In this post I show how to split a URL  and get the file name. Here my URL is http://file1.hpage.com/008142/64/bilder/kajalagarwal1.jpeg now  i want to get the file name from URL.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

SplitActivity.java

public class SplitActivity extends Activity
{

String URL="http://file1.hpage.com/008142/64/bilder/kajalagarwal1.jpeg";

String[] values;

String filename;

@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);

values=URL.split("/");
filename=values[values.length-1];

TextView textView=(TextView)findViewById(R.id.textView1);
textView.setText("file name:");
textView.append(filename);

}
}

No comments:

Post a Comment