2017-06-13 71 views
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; 
    } 
} 

任何帮助,将不胜感激... :)

回答

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(); 

    } 
} 
+0

任何帮助的如何发送文件路径因为我不知道如果我在做它的r ight way ... :)它不会显示任何应用程序 – DarShan

+0

@DarShan检查更新后的代码 –

+0

其实你误解了我的问题......我需要设置Intent数据的帮助......那个文件n =新建文件(环境.getExternalStorageDirectory()+ File.separator +“Sample /”+ FileNameStrings); 看起来不对我..它不返回文件名:( – DarShan