i tried using the android pdf library. Downloaded from https://github.com/jblough/Android-Pdf-Viewer-Library .
Now when i open the pdf it is stuck .. saying pdf page is loading. ut nothing happens.
Here is what i am trying to do .
I have created a class file called qbchem.java and second.java
i have loaded the pdf file in my asset folder. I dont want any one to access these pdf files and that is why i have kept them in my asset folder.
qbchem.java
#
package com.sp.wisdomedutech11;
import java.io.File;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.os.Build;
import net.sf.andpdf.pdfviewer.PdfViewerActivity;
public class qbchem extends Activity {
ListView qbchemlv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qbchem);
qbchemlv = (ListView) findViewById(R.id.qbchemlv1);
String[] values = new String[] { "Chap-1 Basic Concepts",
"Chap-2 Atomic Structure",
"Chap-3 Chemical Bonding",
"Chap-4 Periodic Table"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
qbchemlv.setAdapter(adapter);
qbchemlv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) qbchemlv.getItemAtPosition(position);
if(itemValue.contentEquals("Chap-1 Basic Concepts")){
try {
Intent intent = new Intent(qbchem.this, second.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, "file:///android_asset/chap_1_basic_concept.pdf");
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}}
second.java
package com.sp.wisdomedutech11;
import java.io.File;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.os.Build;
import net.sf.andpdf.pdfviewer.PdfViewerActivity;
public class second extends PdfViewerActivity {
#Override
public int getPreviousPageImageResource() {
// TODO Auto-generated method stub
return R.drawable.left_arrow;
}
#Override
public int getNextPageImageResource() {
// TODO Auto-generated method stub
return R.drawable.right_arrow;
}
#Override
public int getZoomInImageResource() {
// TODO Auto-generated method stub
return R.drawable.zoom_in;
}
#Override
public int getZoomOutImageResource() {
// TODO Auto-generated method stub
return R.drawable.zoom_out;
}
#Override
public int getPdfPasswordLayoutResource() {
// TODO Auto-generated method stub
return R.layout.pdf_file_password;
}
#Override
public int getPdfPageNumberResource() {
// TODO Auto-generated method stub
return R.layout.dialog_pagenumber;
}
#Override
public int getPdfPasswordEditField() {
// TODO Auto-generated method stub
return R.id.etPassword;
}
#Override
public int getPdfPasswordOkButton() {
// TODO Auto-generated method stub
return R.id.btOK;
}
#Override
public int getPdfPasswordExitButton() {
// TODO Auto-generated method stub
return R.id.btExit;
}
#Override
public int getPdfPageNumberEditField() {
// TODO Auto-generated method stub
return R.id.pagenum_edit;
}}
I had had similar issues while trying to get a pdf reader.In your particular case, you cant get the direct location of your pdf from assets. The only thing that(might) work is copying this asset to some other location , and then using the 'file:///' after that.
This might help you copy the file :
Copying a file from withing a apk to the internal storage
see this for copying too :
https://stackoverflow.com/a/19555787/2670775
Related
I want to create application which will write text while listening. I have tried this code but this is not working. and i have followed this link. but it couldn't work for me.
If i speak, it is not displaying spoken words and speech recognition is also not working. if you know the solution Please guide me to solve this problem. Thank you...
package com.kp.notesapp;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.fonts.Font;
import android.graphics.pdf.PdfDocument;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.Environment;
import android.print.PageRange;
import android.provider.DocumentsContract;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.text.Html;
import android.text.style.ParagraphStyle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.compose.ui.text.Paragraph;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.google.android.material.internal.ViewUtils;
import com.kp.notesapp.Models.Notes;
import org.w3c.dom.Document;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
public class NoteView extends AppCompatActivity {
EditText contentEditText;
ImageView micImg;
SpeechRecognizer speechRecognizer;
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_view);
getSupportActionBar().hide();
contentEditText = findViewById(R.id.contentNotes);
micImg = findViewById(R.id.micImg);
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
micImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(count==0){
micImg.setImageResource(R.drawable.micvoice);
speechRecognizer.startListening(speechIntent);
count=1;
}
else{
micImg.setImageResource(R.drawable.mic);
speechRecognizer.stopListening();
count=0;
}
}
});
speechRecognizer.setRecognitionListener(new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle bundle) {
}
#Override
public void onBeginningOfSpeech() {
}
#Override
public void onRmsChanged(float v) {
}
#Override
public void onBufferReceived(byte[] bytes) {
}
#Override
public void onEndOfSpeech() {
}
#Override
public void onError(int i) {
}
#Override
public void onResults(Bundle bundle) {
/*ArrayList<String> data = bundle.getStringArrayList(speechRecognizer.RESULTS_RECOGNITION);
contentEditText.setText(data.get(0));
Log.e("csData", String.valueOf(data));*/
String str = new String();
Log.e( "csonResults " , String.valueOf(bundle));
ArrayList data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < data.size(); i++)
{
Log.e("csresult", String.valueOf(data.get(i)));
str += data.get(i);
}
}
#Override
public void onPartialResults(Bundle bundle) {
ArrayList data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String word = (String) data.get(data.size() - 1);
contentEditText.setText(word);
Log.e("csWord", word);
}
#Override
public void onEvent(int i, Bundle bundle) {
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
saveData();
finish();
}
}
I am having a GridList View in which I am passing data from firebase. When I click on the items it works fine without filtering but when I filter it with searchView then it does not update the index of the list and then provides a different item's context.
I do not know how to update the item while filtering.
Homepage.Activity
package com.project.quizapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import android.app.SearchManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.GridView;
import android.widget.ListAdapter;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static com.project.quizapp.LogIn.catList;
import static com.project.quizapp.LogIn.selected_cat_index_app;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.project.quizapp.CatGridAdapter;
public class HomePage extends AppCompatActivity {
CatGridAdapter adapter;
private GridView catGrid;
private FirebaseFirestore firebasefirestore;
public static List<CategoryModelClass> resultData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
//Binding view and adding toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Subjects");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(HomePage.this, dashActivity.class));
}
});
//Adapter to set all the views
catGrid = findViewById(R.id.catGridview);
adapter = new CatGridAdapter(catList);
catGrid.setAdapter(adapter);
}
//Filtering all tthe views
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflating all the views and filtering
getMenuInflater().inflate(R.menu.search, menu);
MenuItem menuitem = menu.findItem(R.id.searchMenu);
SearchView searchview = (SearchView) menuitem.getActionView();
searchview.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
//Calling filters on the search attempt
//adapter.getFilter().filter(newText);
resultData = new ArrayList<>();
for(CategoryModelClass categoryModelClass:catList){
if(categoryModelClass.getName().toLowerCase().contains(newText.toLowerCase())){
resultData.add(categoryModelClass);
}
adapter = new CatGridAdapter(resultData);
catGrid.setAdapter(adapter);
}
return true;
}
});
return true;
}
//Adding search icon to the toolbar
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if(id == R.id.searchMenu){
return true;
}
return super.onOptionsItemSelected(item);
}
}
CatGridAdapter.java
package com.project.quizapp;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static com.project.quizapp.LogIn.selected_cat_index_app;
import static java.security.AccessController.getContext;
//Creating the adapter wih the default BaseAdapter
//Also implementing
public class CatGridAdapter extends BaseAdapter {
//List of all the class variables
private List<CategoryModelClass> catList;
//Initializing the list wih constructor
public CatGridAdapter(List<CategoryModelClass> catList) {
this.catList = catList;
}
#Override
public int getCount() {
return catList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null){
//Adding all the views as we get the data from firebase
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cat_item_layout,parent,false);
}
else{
view = convertView;
}
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//selected_cat_index_app = position;
// Toast.makeText(parent.getChildAt(position), catList.get(position).getName()+" "+String.valueOf(catList.indexOf(position)), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(parent.getContext(), SetsActivity.class);
parent.getContext().startActivity(intent);
}
});
((TextView) view.findViewById(R.id.catName)).setText(catList.get(position).getName());
Random rnd = new Random();
int color = Color.argb(255,rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255));
view.setBackgroundColor(color);
return view;
}
}
CategoryModelClass.java
package com.project.quizapp;
//Model class to fetch the quiz subjects from the Cloud Firestone
public class CategoryModelClass {
//private variable for ID and the name of
private String id;
private String name;
//constructor
public CategoryModelClass(String id, String name) {
this.id = id;
this.name = name;
}
//methods
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Either implement a Filterable interface in the adapter or update your onQueryTextChange implementation.
Whenever you are typing, every time you are creating a new adapter with filtered data.
try this.. create new method in adapter
public void updateData(List<CategoryModelClass> catList){
this.catList = catList;
notifyDataSetChanged();
}
and call from onQueryTextChange
#Override
public boolean onQueryTextChange(String newText) {
//adapter.getFilter().filter(newText);
resultData = new ArrayList<>();
for (CategoryModelClass categoryModelClass : catList) {
if (categoryModelClass.getName().toLowerCase().contains(newText.toLowerCase())) {
resultData.add(categoryModelClass);
}
adapter.updateData(resultData);
}
return true;
}
I have some dummy problem, I need to get Spinner Item Position from the Fragment to this class:
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Spinner;
import com.example.nortti.politrange.R;
import com.example.nortti.politrange.intefaces.ICatalog;
import com.example.nortti.politrange.objects.Person;
import com.example.nortti.politrange.objects.Site;
import com.example.nortti.politrange.utils.WebApiAdapter;
import com.example.nortti.politrange.views.GeneralFragment;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class PersonCatalog implements ICatalog{
private final String COMMAND_PREFIX = "/api/stats/1";
private final WebApiAdapter apiAdapter = new WebApiAdapter(COMMAND_PREFIX);
private ArrayList<Person> catalogList = new ArrayList<Person>();
private Site site;
public PersonCatalog(Site site) {
this.site = site;
}
#Override
public ArrayList<Person> getCatalogList() {
return catalogList;
}
public void populateData() {
JSONArray jsonObject = null;
try {
jsonObject = (JSONArray)(new JSONParser()).parse(apiAdapter.select(null));
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
catalogList.clear();
Iterator<JSONObject> iterator = jsonObject.iterator();
while(iterator.hasNext()) {
JSONObject o = iterator.next();
catalogList.add(new Person((String)o.get("personName"),(int)(long)o.get("rank")));
}
}
}
I broke my head, I don't know how to do it. Please help! Should I use some Intents or create some getters?
UPD: Fragment Code
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import com.example.nortti.politrange.R;
import com.example.nortti.politrange.adapters.GenAdapter;
import com.example.nortti.politrange.adapters.SiteAdapter;
import com.example.nortti.politrange.intefaces.ICatalog;
import com.example.nortti.politrange.intefaces.impls.PersonCatalog;
import com.example.nortti.politrange.intefaces.impls.SitesCatalog;
import com.example.nortti.politrange.objects.Site;
public class GeneralFragment extends Fragment implements OnClickListener, OnItemSelectedListener {
private Button genApply;
private Spinner spinner;
private ListView genList;
private View header;
private ICatalog siteCatalogImpl;
private ICatalog personCatalogImpl;
public int Num;
public void setSpinnerSource(ICatalog siteCatalogImpl) {
this.siteCatalogImpl = siteCatalogImpl;
spinData();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.general_fragment, container,false);
header = inflater.inflate(R.layout.gen_head, null);
spinner = (Spinner) v.findViewById(R.id.wSpin);
spinner.setOnItemSelectedListener(this);
genApply = (Button) v.findViewById(R.id.genApply);
genApply.setOnClickListener(this);
genList = (ListView) v.findViewById(R.id.genList);
genList.addHeaderView(header);
this.setSpinnerSource(new SitesCatalog());
Intent i = new Intent();
i.putExtra("spin", spinner.getSelectedItemPosition()+1);
return v;
}
private void spinData() {
siteCatalogImpl.populateData();
spinner.setAdapter(new SiteAdapter(getActivity(), siteCatalogImpl.getCatalogList()));
}
private void listData(Site site) {
personCatalogImpl = new PersonCatalog(site);
personCatalogImpl.populateData();
genList.setAdapter(new GenAdapter(getActivity(), personCatalogImpl.getCatalogList()));
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
public void onClick(View v) {
int siteIndex = spinner.getSelectedItemPosition();
switch (v.getId()) {
case R.id.genApply:
listData((Site)siteCatalogImpl.getCatalogList().get(siteIndex));
break;
}
}
}
I calling PersonCatalog at the listdata method.
Try this.
final ArrayList<String> providerlist= new ArrayList<String>();
Spinner spinner1 = (Spinner) findViewById(R.id.prospin);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, providerlist);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = providerlist.get(position);
// Showing selected spinner item
Toast.makeText(this,
"Selected Country : " + item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Why it gives me NULLPointerException on this line
outputStream.write(array);
the WHOLE MainActivity
package com.example.phoneclient;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends Activity {
public Button privateButton;
public Socket CommandSocket,receivingSocket,sendingSocket;
public OutputStream outputStream;
DataOutputStream doDataOutputStream;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
CommandSocket = new Socket("134.129.125.126",62431);
receivingSocket = new Socket("134.129.125.126",52341);
sendingSocket = new Socket("134.129.125.126",42311);
} catch (Exception e) {
// TODO: handle exception
}
privateButton = (Button)findViewById(R.id.private_Mode);
privateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
outputStream = CommandSocket.getOutputStream();
} catch (Exception e) {
// TODO: handle exception
}
String command = "1";
byte[] array = command.getBytes();
System.out.println("byte: "+ array);
try {
outputStream.write(array);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I only want to send this "1" to the server C# by click the button for testing. I don't know why.
If you'd logged the Exception and watches the stacktrace, you would have seen a NetworkOnMainThreadException
You can't open network sockets on the UI thread (in onCreate), so you need to create either an AsyncTask or a Service.
Im implementing a video playback in android im completely new to android, and this is the bit of code i have gathered so far.
according to the logic it should play a video. don't know where im doing wrong.
package com.themetanoia.readfilefromsdc;
import java.io.File;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import android.widget.AdapterView.OnItemClickListener;
public class ReadFileFromSDCActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the file path for external storage
String a = Environment.getExternalStorageDirectory().getAbsolutePath();
final String finalPath = a+"/Videos";
//Define new file function
File f=new File(finalPath);
if(f.isDirectory()){
//Define arrayadapter
setListAdapter(new ArrayAdapter<String>(this, R.layout.listview,files));
//Define listview
ListView listview = getListView();
listview.setTextFilterEnabled(true);
//Onclick list item event
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Uri uri = Uri.parse(finalPath);
MediaController mediaController = new MediaController(getBaseContext());
mediaController.setAnchorView(mVideoView);
Uri video = Uri.parse(finalPath+"/"+selectedValue);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoURI(video);
mVideoView.start();
}
});
}
}
}
<VideoView
android:id="#+id/videoView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
VideoView vv = (VideoView) findViewById(R.id.videoView1);
uri = "your video uri";
vv.setVideoURI(Uri.parse(uri));
vv.start();
vv.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
finish();
}
});