Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 60 additions & 35 deletions src/android/GalleryRefresh.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,88 @@
package com.ung.galleryrefresh;

import org.apache.cordova.CordovaPlugin;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.webkit.MimeTypeMap;
import org.apache.cordova.CallbackContext;

import java.io.File;

import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import java.io.File;

/**
* This class echoes a string called from JavaScript.
*/
public class GalleryRefresh extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try {
if (action.equals("refresh")) {
String filePath = _checkFilePath(args.getString(0));

if (action.equals("refresh")) {
String filePath = _checkFilePath(args.getString(0));
File file = new File(filePath);

if (filePath.equals("")) {
callbackContext.error("Invalid File Path");
}
if (!file.exists()) {
callbackContext.error("Invalid File Path");
return false;
}

File file = new File(filePath);

this._scanPhoto(file);
}
this._scanFile(file);
}

callbackContext.success("Success Scan File");
return true;
callbackContext.success("Success Scan File");
return true;
} catch (Exception e) {
callbackContext.error(e.getMessage());
return false;
callbackContext.error(e.getMessage());
return false;
}
}

private void _scanPhoto(File imageFile) {
private void _scanFile(File contentFile) {
Uri contentUri = Uri.fromFile(contentFile);

// deprecated
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(imageFile);
mediaScanIntent.setData(contentUri);
cordova.getActivity().sendBroadcast(mediaScanIntent);

// new using MediaStore
String mimeType;
if (ContentResolver.SCHEME_CONTENT.equals(contentUri.getScheme())) {
mimeType = cordova.getActivity().getContentResolver().getType(contentUri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(contentUri.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension.toLowerCase());
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, contentFile.getAbsolutePath());
Uri externalContentUri = null;
if (mimeType != null) {
values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
if (mimeType.contains("audio/")) {
externalContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
} else if (mimeType.contains("image/")) {
externalContentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if (mimeType.contains("video/")) {
externalContentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else {
if (Build.VERSION.SDK_INT >= 29) {
externalContentUri = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
}
}
}
if (externalContentUri != null) {
cordova.getActivity().getContentResolver().insert(externalContentUri, values);
}
}

private String _checkFilePath(String filePath) {
String return_value = "";

try {
return_value = filePath.replaceAll("^file://", "");
} catch (Exception e) {
throw new RuntimeException("Error transfering file, error: " + e.getMessage());
}
return return_value;
try {
return filePath.replaceAll("^file://", "");
} catch (Exception e) {
throw new RuntimeException("Error transferring file, error: " + e.getMessage());
}
}
}