Guest User

Untitled

a guest
Oct 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
  2. Uri.parse("file://" + Environment.getExternalStorageDirectory())));
  3.  
  4. E/AndroidRuntime( 2628): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=2628, uid=10068
  5.  
  6. MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
  7. /*
  8. * (non-Javadoc)
  9. * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
  10. */
  11. public void onScanCompleted(String path, Uri uri)
  12. {
  13. Log.i("ExternalStorage", "Scanned " + path + ":");
  14. Log.i("ExternalStorage", "-> uri=" + uri);
  15. }
  16. });
  17.  
  18. MediaScannerConnection.scanFile(ApplicationContext.context, new String[] { imageFile.getPath() }, null,
  19. new MediaScannerConnection.OnScanCompletedListener() {
  20. @Override
  21. public void onScanCompleted(String path, Uri uri) {
  22. Log.i(TAG, "Scanned " + path);
  23. }
  24. });
  25.  
  26. sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your image path"))));
  27.  
  28. MediaScannerConnection conn;
  29.  
  30. public void startScan(String url) {
  31. imagepath = url;
  32. if (conn != null)
  33. conn.disconnect();
  34. conn = new MediaScannerConnection(activity.this, activity.this);
  35. conn.connect();
  36. }
  37.  
  38. @Override
  39. public void onMediaScannerConnected() {
  40. try {
  41. conn.scanFile(imagepath, getMimeType(imagepath));
  42. } catch (java.lang.IllegalStateException e) {
  43. //Do something
  44. }
  45. }
  46.  
  47. @Override
  48. public void onScanCompleted(String path, Uri uri) {
  49. conn.disconnect();
  50. }
  51.  
  52. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  53. Intent mediaScanIntent = new Intent(
  54. Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  55. Uri contentUri = Uri.fromFile(out); //out is your file you saved/deleted/moved/copied
  56. mediaScanIntent.setData(contentUri);
  57. this.sendBroadcast(mediaScanIntent);
  58. } else {
  59. sendBroadcast(new Intent(
  60. Intent.ACTION_MEDIA_MOUNTED,
  61. Uri.parse("file://"
  62. + Environment.getExternalStorageDirectory())));
  63. }
  64.  
  65. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  66. Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  67. File f1 = new File("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
  68. Uri contentUri = Uri.fromFile(f1);
  69. mediaScanIntent.setData(contentUri);
  70. sendBroadcast(mediaScanIntent);
  71. } else {
  72. sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
  73. }
  74. sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your image path"))));
  75.  
  76. MediaScannerConnection.MediaScannerConnectionClient(
  77. {
  78. @Override
  79. public void onScanCompleted(String path, Uri uri) {
  80.  
  81. if (path.equals(**your filename**.getAbsolutePath()))
  82. {
  83. Log.i("Scan Status", "Completed");
  84. Log.i("uri: ",uri.toString());
  85.  
  86. conn.disconnect();
  87. }
  88. }
  89. @Override
  90. public void onMediaScannerConnected()
  91. {
  92. // TODO Auto-generated method stub
  93. conn.scanFile(**your file name**.getAbsolutePath(),null);
  94. }
  95. });
  96.  
  97. conn.connect();
  98.  
  99. FileOutputStream out = new FileOutputStream(file);
  100. finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
  101. out.flush();
  102. //Gallery Refresh Code
  103.  
  104. MediaScannerConnection.scanFile(getActivity(), new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
  105. /*
  106. * (non-Javadoc)
  107. * @see android.media.MediaScannerConnection.OnScanCompletedListener#onScanCompleted(java.lang.String, android.net.Uri)
  108. */
  109. public void onScanCompleted(String path, Uri uri)
  110. {
  111. Log.i("ExternalStorage", "Scanned " + path + ":");
  112. Log.i("ExternalStorage", "-> uri=" + uri);
  113. }
  114. });
  115. out.close();
Add Comment
Please, Sign In to add comment