我的应用程序中有一个Gridview ... Gridview显示特定文件夹中的所有可用文件,例如“示例文件夹”...
我正在尝试的是在项目点击时传递ACTION_VIEW Intent但我无法设置其数据和类型...... 大多数文件都是图像或视频......
以下是活动代码 -
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;
}
}
任何帮助将不胜感激......:)
答案 0 :(得分:0)
试试这个
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
// No need to set both Type & Data. if you set one itself enough
File n = new File(Environment.getExternalStorageDirectory()+ File.separator + "Sample/" + FileNameStrings);
i.setType(MimeTypeMap.getFileExtensionFromUrl(n.getAbsolutePath()));
startActivity(Intent.createChooser(i, "Choose an App"));
已更新
尝试打开文件
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(fileExt(file).substring(1));
newIntent.setDataAndType(Uri.fromFile(file),mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(newIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No handler for this type of file.", Toast.LENGTH_LONG).show();
}
private String fileExt(String url) {
if (url.indexOf("?") > -1) {
url = url.substring(0, url.indexOf("?"));
}
if (url.lastIndexOf(".") == -1) {
return null;
} else {
String ext = url.substring(url.lastIndexOf(".") + 1);
if (ext.indexOf("%") > -1) {
ext = ext.substring(0, ext.indexOf("%"));
}
if (ext.indexOf("/") > -1) {
ext = ext.substring(0, ext.indexOf("/"));
}
return ext.toLowerCase();
}
}