Guest User

Untitled

a guest
Dec 11th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. private void showFileChooser() {
  2. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  3. intent.addCategory(Intent.CATEGORY_OPENABLE);
  4. intent.setType("*/*");
  5. startActivityForResult(intent, PICK_FILE_REQUEST);
  6. }
  7.  
  8. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  9.  
  10. if (requestCode == PICK_FILE_REQUEST && resultCode == RESULT_OK) {
  11. if (resultCode != RESULT_CANCELED) {
  12. if (data == null) {
  13. //no data present
  14. return;
  15. }
  16.  
  17.  
  18. Uri selectedFileUri = data.getData();
  19.  
  20. Log.i("suraj", " FuelExpensesActivity Selected Uri Path:" + selectedFileUri);
  21. String selectedFilePath = FilePath.getPath(this, selectedFileUri);
  22. Log.i("suraj", "FuelExpensesActivity Selected File Path:" + selectedFilePath);
  23.  
  24. if (selectedFilePath != null && !selectedFilePath.equals("")) {
  25.  
  26. TextView tvFileName = new TextView(this);
  27. tvFileName.setLayoutParams(new android.view.ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  28.  
  29. tvFileName.setPadding(5, 5, 5, 5);
  30. tvFileName.setTextColor(Color.parseColor("#FFFFFF"));
  31. tvFileName.setText(getFileName(selectedFileUri));
  32. parent_layout_doc.addView(tvFileName);
  33. parent_layout_doc.setPadding(5, 5, 5, 5);
  34. imgUrl.add(selectedFilePath);
  35.  
  36. } else {
  37. Toast.makeText(this, "file not found.....", Toast.LENGTH_SHORT).show();
  38. }
  39. }
  40.  
  41. java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=11, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/raw:/storage/emulated/0/Download/harley-davidson-vrod-tecnobike-img01~2.jpg flg=0x1 }} to activity {com.riya.product.intranet/com.riya.product.allwance_criteria.FoodActivity}: java.lang.NumberFormatException: For input string: "raw:/storage/emulated/0/Download/harley-davidson-vrod-tecnobike-img01~2.jpg"
  42. at android.app.ActivityThread.deliverResults(ActivityThread.java:4517)
  43. at android.app.ActivityThread.handleSendResult(ActivityThread.java:4560)
  44. at android.app.ActivityThread.-wrap19(Unknown Source:0)
  45. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
  46. at android.os.Handler.dispatchMessage(Handler.java:105)
  47. at android.os.Looper.loop(Looper.java:164)
  48. at android.app.ActivityThread.main(ActivityThread.java:6798)
  49. at java.lang.reflect.Method.invoke(Native Method)
  50. at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
  51. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  52. Caused by: java.lang.NumberFormatException: For input string: "raw:/storage/emulated/0/Download/harley-davidson-vrod-tecnobike-img01~2.jpg"
  53. at java.lang.Long.parseLong(Long.java:590)
  54. at java.lang.Long.valueOf(Long.java:804)
  55. at methodclass.FilePath.getPath(FilePath.java:46)
  56. at com.riya.product.allwance_criteria.FoodActivity.onActivityResult(FoodActivity.java:1415)
  57. at android.app.Activity.dispatchActivityResult(Activity.java:7272)
  58. at android.app.ActivityThread.deliverResults(ActivityThread.java:4513)
  59. at android.app.ActivityThread.handleSendResult(ActivityThread.java:4560) 
  60. at android.app.ActivityThread.-wrap19(Unknown Source:0) 
  61. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744) 
  62. at android.os.Handler.dispatchMessage(Handler.java:105) 
  63. at android.os.Looper.loop(Looper.java:164) 
  64. at android.app.ActivityThread.main(ActivityThread.java:6798) 
  65. at java.lang.reflect.Method.invoke(Native Method) 
  66. at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
  67. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
  68.  
  69. public class FilePath {
  70.  
  71. public static String getPath(final Context context, final Uri uri) {
  72.  
  73. final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  74.  
  75. // DocumentProvider
  76. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  77.  
  78. if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  79.  
  80. // ExternalStorageProvider
  81. if (isExternalStorageDocument(uri)) {
  82. final String docId = DocumentsContract.getDocumentId(uri);
  83. final String[] split = docId.split(":");
  84. final String type = split[0];
  85.  
  86. if ("primary".equalsIgnoreCase(type)) {
  87. return Environment.getExternalStorageDirectory() + "/"
  88. + split[1];
  89. }
  90.  
  91. // TODO handle non-primary volumes
  92. }
  93. // DownloadsProvider
  94. else if (isDownloadsDocument(uri)) {
  95.  
  96. final String id = DocumentsContract.getDocumentId(uri);
  97. final Uri contentUri = ContentUris.withAppendedId(
  98. Uri.parse("content://downloads/public_downloads"),
  99. Long.valueOf(id));
  100.  
  101. return getDataColumn(context, contentUri, null, null);
  102. }
  103. // MediaProvider
  104. else if (isMediaDocument(uri)) {
  105. final String docId = DocumentsContract.getDocumentId(uri);
  106. final String[] split = docId.split(":");
  107. final String type = split[0];
  108.  
  109. Uri contentUri = null;
  110. if ("image".equals(type)) {
  111. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  112. } else if ("video".equals(type)) {
  113. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  114. } else if ("audio".equals(type)) {
  115. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  116. }
  117.  
  118. final String selection = "_id=?";
  119. final String[] selectionArgs = new String[] { split[1] };
  120.  
  121. return getDataColumn(context, contentUri, selection,
  122. selectionArgs);
  123. }
  124. }
  125. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  126. return getDataColumn(context, uri, null, null);
  127. }else if ("file".equalsIgnoreCase(uri.getScheme())) {
  128. return uri.getPath();
  129. }
  130. // MediaStore (and general)
  131.  
  132. }
  133. return null;
  134.  
  135. }
  136.  
  137. public static String getDataColumn(Context context, Uri uri,
  138. String selection, String[] selectionArgs) {
  139.  
  140. Cursor cursor = null;
  141. final String column = "_data";
  142. final String[] projection = { column };
  143.  
  144. try {
  145. cursor = context.getContentResolver().query(uri, projection,
  146. selection, selectionArgs, null);
  147. if (cursor != null && cursor.moveToFirst()) {
  148. final int column_index = cursor.getColumnIndexOrThrow(column);
  149. return cursor.getString(column_index);
  150. }
  151. } finally {
  152. if (cursor != null)
  153. cursor.close();
  154. }
  155. return null;
  156. }
  157.  
  158. public static boolean isExternalStorageDocument(Uri uri) {
  159. return "com.android.externalstorage.documents".equals(uri
  160. .getAuthority());
  161. }
  162.  
  163.  
  164. public static boolean isDownloadsDocument(Uri uri) {
  165. return "com.android.providers.downloads.documents".equals(uri
  166. .getAuthority());
  167. }
  168.  
  169.  
  170. public static boolean isMediaDocument(Uri uri) {
  171. return "com.android.providers.media.documents".equals(uri
  172. .getAuthority());
  173. }
  174.  
  175. else if (isDownloadsDocument(uri)) {
  176.  
  177.  
  178. final String id = DocumentsContract.getDocumentId(uri);
  179. if (!TextUtils.isEmpty(id)) {
  180. if (id.startsWith("raw:")) {
  181. return id.replaceFirst("raw:", "");
  182. }
  183. try {
  184. final Uri contentUri = ContentUris.withAppendedId(
  185. Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  186. return getDataColumn(context, contentUri, null, null);
  187. } catch (NumberFormatException e) {
  188. e.printStackTrace();
  189. Log.e("suraj", "Exception in FilePath "+ e.getMessage());
  190. return null;
  191. }
  192. }
  193. }
Add Comment
Please, Sign In to add comment