0
我有一個GridView在我的應用程序... GridView控件顯示一個特定文件夾中所有可用的文件,讓說,「示例文件夾」 ......如何將數據從Gridview項傳遞給Android?
什麼我想是通過對項目的意向ACTION_VIEW單擊但我不能設置它的數據和類型... 大多數文件或者是圖像或視頻...
下面的代碼的活動 -
public class Downloaded extends AppCompatActivity {
// Declare variables
private String[] FilePathStrings;
private String[] FileNameStrings;
private File[] listFile;
GridView grid;
GridViewAdapter adapter;
File file;
TextView empty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
// Check for SD Card
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(Downloaded.this, "Error! No SDCARD Found!", Toast.LENGTH_LONG)
.show();
} else {
// Locate the image folder in your SD Card
file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Sample");
file.mkdirs();
}
if (file.isDirectory()) {
listFile = file.listFiles();
// Create a String array for FilePathStrings
FilePathStrings = new String[listFile.length];
// Create a String array for FileNameStrings
FileNameStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++) {
// Get the path of the image file
FilePathStrings[i] = listFile[i].getAbsolutePath();
// Get the name image file
FileNameStrings[i] = listFile[i].getName();
}
}
// Locate the GridView in gridview_main.xml
grid = (GridView) findViewById(R.id.gridview);
empty = (TextView) findViewById(R.id.empty);
empty.setText("You haven't saved any Pic yet...!");
grid.setEmptyView(empty);
// Pass String arrays to LazyAdapter Class
adapter = new GridViewAdapter(Downloaded.this, FilePathStrings, FileNameStrings);
// Set the LazyAdapter to the GridView
grid.setAdapter(adapter);
// Capture gridview item click
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
//Don't Know what to Do Here :(
File n = new File(Environment.getExternalStorageDirectory()+ File.separator + "Sample/" + FileNameStrings);
i.setDataAndType(Uri.fromFile(n), "*/*");
startActivity(Intent.createChooser(i, "Choose an App"));
}
});
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
break;
}
return true;
}
}
而這裏的適配器 -
public class GridViewAdapter extends BaseAdapter {
// Declare variables
private Activity activity;
String[] filepath;
String[] filename;
Context mContext;
private static LayoutInflater inflater = null;
public GridViewAdapter(Context c) {
mContext = c ;
}
public GridViewAdapter(Activity a, String[] fpath, String[] fname) {
activity = a;
this.filepath = fpath;
filename = fname;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return this.filepath.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.gridview_item, null);
// Locate the ImageView in gridview_item.xml
ImageView img = (ImageView) vi.findViewById(R.id.image);
// Set file name to the TextView followed by the position
TextView txt = (TextView) vi.findViewById(R.id.name);
// Decode the filepath with BitmapFactory followed by the position
// Set the decoded bitmap into ImageView
Glide.with(activity)
.load(filepath[position])
.into(img);
txt.setText(filename[position]);
return vi;
}
}
任何幫助,將不勝感激... :)
任何幫助的如何發送文件路徑因爲我不知道如果我在做它的r ight way ... :)它不會顯示任何應用程序 – DarShan
@DarShan檢查更新後的代碼 –
其實你誤解了我的問題......我需要設置Intent數據的幫助......那個文件n =新建文件(環境.getExternalStorageDirectory()+ File.separator +「Sample /」+ FileNameStrings); 看起來不對我..它不返回文件名:( – DarShan