简体   繁体   中英

Android - Java - No Activity Found to Handle Intent

I am trying to create an Android / Java plugin for the cross-platform program Phonegap / Cordova 3.2. I am following several tutorials but can't get the simplest plugin to work.

Currently I am working on the idea that my Java code is just wrong somewhere.

Could someone please review the following code and advise if there is something obviously wrong?

The error I keep getting is

Exception: No Activity found to handle Intent { act=android.intent.action.MEDIA_SCANNER_SCAN_FILE dat=file:///{"fullPath":"media\/test.mp3"} }

Here is my .java file

package org.media.scan;

import java.io.File;

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

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

public class Scan extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    try {
        if ( action.equals("addRemove") ) {

            String filePath = args.getString(0);

            filePath = filePath.replaceAll("^file://", "");

            if (filePath.equals("")) {
                callbackContext.error("null path passed");
                return false;
            }               

            File file = new File(filePath);

            Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            scanIntent.setData(Uri.fromFile(file));

            this.cordova.getActivity().startActivity( scanIntent );
            callbackContext.success("good");
            return true;

        } else {
            callbackContext.error("invalid action phrase");

        }

        return false;

    } catch(Exception e) {

        System.err.println("Exception: " + e.getMessage());
        callbackContext.error(e.getMessage());
        return false;
    }




}

}

I am calling my Java code with this .js code

var Scan = {
createEvent:function (fullPath, successCallback, errorCallback) {
    cordova.exec(
        successCallback, // success callback function
        errorCallback, // error callback function
        'Scan', // mapped to our native Java class
        'addRemove', // with this action name
        [
            {                  
                "fullPath":fullPath
            }
        ]
    );
}
}

module.exports = Scan;

It's a broadcast action not activity action, you should use the send broadcast method for this kind of action!

http://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_SCANNER_SCAN_FILE

This is the wrong line in code " this.cordova.getActivity().startActivity( scanIntent ); "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM