I have the following codes to browse files from SD Card in Android.
But how do I implement codes such that the selected file can be uploaded to a specified bucket in Amazon S3? Please help, thank you very much.
package sg.edu.nyp.sit.s3;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class FilePickS3Activity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root="/";
private TextView myPath;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPath = (TextView)findViewById(R.id.path);
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
}
if you want to transfer the data programmatically then you can use the AWS SDK:
http://aws.amazon.com/de/sdkforandroid/
There is a online documentation with examples: http://aws.amazon.com/articles/4225549089557252
Related
I'm trying a code of viewing the items inside of a directory, but every time I click the button where the code is coming from, the app closes and gives me an error saying "Unfortunately, YourApp has stopped.", and I don't know why is that.
Here is the code I'm trying:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.Environment;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ViewTextFiles extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewtextfiles);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();
}
}
P.S I'm trying the code in a real device but without the use of usb cable (I export my .apk from my unit) because I'm having a problem with my unit which doesn't recognize my device when its connected through the usb cable, and as same as with the emulator, its having a problem with my processor which is I don't know how to fix.
You have to provide this permission in android manifest file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Your Activity class should be as following:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
TextView myPath;
private List<String> item = null;
private List<String> path = null;
private String root;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.txt);
list = (ListView) findViewById(R.id.listView);
list.setOnItemClickListener(this);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item);
setListAdapter(fileList);
}
private void setListAdapter(ArrayAdapter<String> adapter) {
list.setAdapter(adapter);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();
}
}
}
Your activity_main.xml file should be as following:
<?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/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</LinearLayout>
I'm new to android and I need the image from the users choice after choosing the image from any location to be the image of the image button. Can anyone provide me with a sample code to help me out? Thanks.
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file_explorer);
myPath = (TextView)findViewById(R.id.action_settings);
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.file_row, item);
setListAdapter(fileList);
}
protected void onListItemClick(ListView l, View v, int position, long id, String[] writeinfo, Options options) {
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.mufc)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
}
Another thing, I am also not sure of how to save the image. Thanks.
Set an item clicked listener on the list view. http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
On its callback you will probably be able to get the file info needed. Then it's just a matter of setting the drawable on the button. http://developer.android.com/reference/android/widget/ImageView.html#setImageBitmap(android.graphics.Bitmap)
this is my file browser code below when show files from sd card i put multiple apk files in sd card but this code just show apk files in sd card not install when click what do ido? please help me. i want to run apk files which show on listview but is not run how do i modify this code to run selected file??
public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();
}
}
}
Implement a ListView (follow this TUTORIAL).
Then implement a listener to handle the click on the items ( listView.onItemClickListener(); )
and then use the snipper below for the installation:
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///path/to/your.apk"))
.setType("application/vnd.android.package-archive");
startActivity(promptInstall);
ref: https://stackoverflow.com/a/4604922/847818
You have to go step-by-step.
I commented out the section I was adding protected boolean onLongListItemClick(ListView l, View v, int position, long id).
If I un-comment I get an exception. I am trying to add if there is long press I will delete the file, which will be added later. Is there a simple way to do this.
public class AndroidExplorer extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private TextView myPath;
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Boot");
String root2 = myFile.getAbsolutePath();;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPath = (TextView)findViewById(R.id.path);
getDir(root2);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root2))
{
item.add("Return to Boot Directory");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
// protected boolean onLongListItemClick(ListView l, View v, int position, long id) {
// // Log.i(TAG, "onLongListItemClick id=" + id);
// Toast.makeText(this,
// "I will be terminated",
// Toast.LENGTH_LONG).show();
// return true;
// }
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else
{
String fileName = file.getName();
String fname="";
String ext="";
int mid= fileName.lastIndexOf(".");
fname=fileName.substring(0,mid);
ext=fileName.substring(mid+1,fileName.length());
if(ext.equals("jpg"))
{
Toast.makeText(this,
"view the jpg",
Toast.LENGTH_LONG).show();
}
if(ext.equals("3gp"))
{
Toast.makeText(this,
"play the video",
Toast.LENGTH_LONG).show();
}
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
}
add an AdapterView.OnItemLongClickListener. See setOnItemLongClickListener.
.
listView.setOnItemLongClickListener (new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
//do your stuff here
}
});
I want to display the file name which is residing in specific location in sdcard. is it possible to display it in listview format? pls guide me.
This is the stuff,
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File("/sdcard/");
// or you can use File f=new File(Environment.getExternalStorageDirectory().getPAth());
File[] files = f.listFiles();
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
For more detail look at: Implement a simple File Explorer in Android .
If you know the location/path of the directory from where you want to get the files from, then you can use the following code to get the list of files and display them in list view.
//Assuming that MEDIA_PATH is the location from where the files are to be retrieved.
List<String> songs=new ArrayList<String>();
File home = new File(MEDIA_PATH); // Getting file representation of the directory
File[] musicFiles=home.listFiles();
if(musicFiles.length>0){
for( File f:musicFiles) { songs.add(f.getName()); }
ListAdapter adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,songs);
setListAdapter(adapter);
}
If you are using ListActivity, you wouldn't need to use setContentView(...).
You can also use a FilenameFilter, if you want only specific type of files(e.g mp3 files) to be fetched and displayed.
Use this code,
public class RecordedVideos extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root="/sdcard/Recorded Videos";
private TextView myPath;
Button back;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filelist);
back=(Button)findViewById(R.id.back);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
File file = new File(path.get(arg2));
boolean b=file.delete();
return false;
}
});
myPath = (TextView)findViewById(R.id.path);
getDir(root);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("..../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else
{
/*new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show(); */
Intent intent= new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri filetype= Uri.parse("file://" + file.getPath());
String filename=file.getName();
if(filename.endsWith(".txt")||filename.endsWith(".doc"))
{
intent.setDataAndType(filetype, "text/*");
startActivity(intent);
}
else if(filename.endsWith(".gif")||filename.endsWith(".jpg")||filename.endsWith(".png"))
{
intent.setDataAndType(filetype, "image/*");
startActivity(intent);
}
else if(filename.endsWith(".mp4")||filename.endsWith(".3gp"))
{
intent.setDataAndType(filetype, "video/*");
startActivity(intent);
}
else
{
intent.setDataAndType(filetype, "audio/*");
startActivity(intent);
}
}
}
}