2015-11-02 281 views
1

我试图实现的目标。我有一个button,当按钮被点击时,应用程序打开一个文件选择器,用户选择一个文件。该应用程序然后使用FileInputStream来读取文件并生成byte[]。我在button下面有一个TextView,然后它将只显示byte[].length。下面是在button.onClick()事件中的代码:Android从onActivityResult获取文件名和路径

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 
    intent.setType("*/*"); 

    requestFilePickerCode = parent.registerActivityResultListener(this); 
    try 
    { 
     parent.startActivityForResult(intent, requestFilePickerCode); 
    } 
    catch (ActivityNotFoundException e) 
    { 
     Toast.makeText(task.getParent(), "Please install a file manager", Toast.LENGTH_SHORT).show(); 
    } 

现在这个代码的工作,我已经证实,它激发onActivityResult选择文件时。我简单地打印Log显示data.toString()产生以下输出:

11-02 15:14:36.196 2535-2535/? V/class za.co.gpsts.gpsjobcard.utility.handlers.PebbleTypeHandlerBinary: -----> content:/com.android.providers.downloads.documents/document/1 

所以似乎越来越选定的文件。当我运行的应用程序,我选择一个文件,它抛出我的自定义错误:

11-02 15:14:36.196 2535-2535/? E/class za.co.gpsts.gpsjobcard.utility.handlers.PebbleTypeHandlerBinary: -----> File does not exist 

这显然表明,我没有得到的文件。这里是我的代码:

@Override 
public boolean onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    byte[] fileContent; 

    // check that data is not null and assign to file if not null 
    if (data != null) 
    { 
     Uri uri = data.getData(); 
     String uriString = uri.toString(); 
     file = new File(uriString); 

     Log.v(PebbleTypeHandlerBinary.class.toString(), "-----> " + file.toString()); 

     // declare file input stream and read bytes 
     // write to string variable to test and test output 
     FileInputStream fin = null; 
     try 
     { 
      fin = new FileInputStream(file); 
      fileContent = new byte[(int) file.length()]; 

      fin.read(fileContent); 
      String test = new String(fileContent); 
      Log.v(PebbleTypeHandlerBinary.class.toString(), "=====> " + test); 
     } 
     catch (FileNotFoundException e) 
     { 
      Toast.makeText(task.getParent(), "File not found", Toast.LENGTH_SHORT).show(); 
      Log.e(PebbleTypeHandlerBinary.class.toString(), "-----> File does not exist"); 
     } 
     catch (IOException e) 
     { 
      Toast.makeText(task.getParent(), "Error reading file", Toast.LENGTH_SHORT).show(); 
      Log.e(PebbleTypeHandlerBinary.class.toString(), "-----> Error while reading the file"); 
     } 
     finally 
     { 
      // close the file input stream to stop mem leaks 
      try 
      { 
       if (fin != null) 
       { 
        fin.close(); 
       } 
      } catch (IOException e) 
      { 
       Log.e(PebbleTypeHandlerBinary.class.toString(), "-----> Error closing the stream"); 
      } 
     } 

     Log.v(PebbleTypeHandlerBinary.class.toString(), data.toString()); 
    } 

    return false; 
} 

请你们可以查看我的代码,并帮助我得到这个工作。任何帮助,将不胜感激。

回答

-1

U可搜索将uri转换为filepath

GetData() retruns a uri。

但是​​需要filepath参数;

像这样:

public static String getRealFilePath(final Context context, final Uri uri) { 
    if (null == uri) return null; 
    final String scheme = uri.getScheme(); 
    String data = null; 
    if (scheme == null) 
     data = uri.getPath(); 
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) { 
     data = uri.getPath(); 
    } else if 
(ContentResolver.SCHEME_CONTENT.equals(scheme)) { 

Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null, null); 
     if (null != cursor) { 
      if (cursor.moveToFirst()) { 

int index = cursor.getColumnIndex(ImageColumns.DATA); 
       if (index > -1) { 
        data = cursor.getString(index); 
       } 
      } 
      cursor.close(); 
     } 
    } 
    return data; 
} 
+0

谢谢你指点我在正确的方向。我现在已将代码更改为'\t \t \t Uri uri = data.getData(); \t \t \t String uriString = uri.getPath(); \t \t \t file = new File(uriString); \t \t \t // String path = file.getAbsolutePath(); \t \t \t Log.v(PebbleTypeHandlerBinary.class.toString(),“----->”+ uriString);'。这仍然会抛出文件未找到... –

+0

我已经设法改变代码来获取路径感谢,但它仍然会抛出文件未找到。堆栈跟踪现在按以下方式打印路径:11-02 19:21:57.073 7506-7506 /? V/class za.co.gpsts.gpsjobcard.utility.handlers.PebbleTypeHandlerBinary:----->/document/2 –

+0

你不能简单地使用uri.getPath()获取文件路径 –

1

我设法如下解决它:

我以前inputStream = task.getParent().getContentResolver().openInputStream(uri);得到一个InputStream。然后使用ByteArrayOutputStream写入一个字节[]。见下面的代码。

@Override 
public boolean onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    Uri uri = data.getData(); 
    byte[] fileContent; 
    InputStream inputStream = null; 

    try 
    { 
     inputStream = task.getParent().getContentResolver().openInputStream(uri); 
     if (inputStream != null) 
     { 
      fileContent = new byte[(int)file.length()]; 
      inputStream.read(fileContent); 
      fileContent = new byte[1024]; 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      int read; 
      while((read=inputStream.read(fileContent))>-1) baos.write(fileContent,0,read); 
      fileContent = baos.toByteArray(); 
      baos.close(); 
      Log.v(PebbleTypeHandlerBinary.class.toString(), "-----> Input Stream: " + inputStream); 
      Log.v(PebbleTypeHandlerBinary.class.toString(), "-----> Byte Array: " + fileContent.length); 
     } 
     else 
     { 
      Log.e(PebbleTypeHandlerBinary.class.toString(), "-----> Input Stream is null"); 
     } 
    } 
    catch (FileNotFoundException e) 
    { 
     Log.e(PebbleTypeHandlerBinary.class.toString(), "-----> File not found", e); 
    } 
    catch (IOException e) 
    { 
     Log.e(PebbleTypeHandlerBinary.class.toString(), "-----> Error reading file", e); 
    } 
    finally 
    { 
     if (inputStream != null) 
     { 
      try 
      { 
       inputStream.close(); 
      } 
      catch (IOException e) 
      { 
       Log.e(PebbleTypeHandlerBinary.class.toString(), "-----> Error reading file", e); 
      } 
     } 
    } 

    return false; 
} 

感谢您的帮助。