如何使用JNI实现文件操作?

时间:2017-06-07 13:54:34

标签: android java-native-interface

我需要对连接到我的Android手机的USB设备执行文件操作。应该通过android java代码授予权限,并且应该使用JNI执行文件操作。目前我无法授予执行文件操作的权限 - 我得到了一个EACCESS:权限被拒绝错误

我在这里附上了我的代码:

public class MainActivity extends AppCompatActivity {

private UsbAccessory accessory;
private String TAG = "TAG";
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private PendingIntent mPermissionIntent;
private UsbManager manager;
private UsbDeviceConnection connection;
private HashMap<Integer, Integer> connectedDevices;
TextView tv;



// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Example of a call to a native method
    tv = (TextView) findViewById(R.id.sample_text);
   // tv.setText(stringFromJNI());

    connectedDevices = new HashMap<Integer, Integer>();

    manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    registerReceiver(usbManagerBroadcastReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));
    registerReceiver(usbManagerBroadcastReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
    registerReceiver(usbManagerBroadcastReceiver, new IntentFilter(ACTION_USB_PERMISSION));

    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

    /*String str = ndkopenfile();
    tv.setText(str);*/

    final Handler handler = new Handler();

    handler.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            checkForDevices();
        }
    }, 1000);
}

/**
 * A native method that is implemented by the 'native-lib' native library,
 * which is packaged with this application.
 */

// public native String stringFromJNI();

@Override
public void onDestroy()
{
    super.onDestroy();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
}

// private static native void notifyDeviceAttached(int fd);
 //private static native void notifyDeviceDetached(int fd);

public  native String ndkopenfile();

private final BroadcastReceiver usbManagerBroadcastReceiver = new BroadcastReceiver()
{
    public void onReceive(Context context, Intent intent)
    {
        try
        {
            String action = intent.getAction();

            Log.d(TAG, "INTENT ACTION: " + action);

            if (ACTION_USB_PERMISSION.equals(action))
            {
                Log.d(TAG, "onUsbPermission");

                synchronized (this)
                {
                    UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
                    {
                        if(device != null)
                        {


                          /*  int fd = connectToDevice(device);
                            Log.d(TAG,"device file descriptor: " + fd);
                           // notifyDeviceAttached(fd);
                            String str = ndkopenfile();
                            tv.setText(str);*/

                          writetofile();
                        }
                    }
                    else
                    {
                        Log.d(TAG, "permission denied for device " + device);
                    }
                }
            }

            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action))
            {
                Log.d(TAG, "onDeviceConnected");

                synchronized(this)
                {
                    UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (device != null)
                    {
                        manager.requestPermission(device, mPermissionIntent);
                    }
                }
            }

            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action))
            {
                Log.d(TAG, "onDeviceDisconnected");

                synchronized(this)
                {
                    UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    int fd = connectedDevices.get(device.getDeviceId());

                    Log.d(TAG, "device: " + device.getDeviceId() + " disconnected. fd: " + fd);

                   // notifyDeviceDetached(fd);

                    connectedDevices.remove(device.getDeviceId());
                }
            }
        }
        catch(Exception e)
        {
            Log.d(TAG, "Exception: " + e);
        }
    }


};

private void writetofile() {

   // File file = new File("/storage/9E6D-8A07/COMMWR");

    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("/storage/9E6D-8A07/COMMWR", Context.MODE_PRIVATE));
        outputStreamWriter.write("Hello");
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }

}

private int connectToDevice(UsbDevice device)
{
    connection = manager.openDevice(device);
    // if we make this, kernel driver will be disconnected
    connection.claimInterface(device.getInterface(0), true);

    Log.d(TAG, "inserting device with id: " + device.getDeviceId() + " and file descriptor: " + connection.getFileDescriptor());
    connectedDevices.put(device.getDeviceId(), connection.getFileDescriptor());

    return connection.getFileDescriptor();
}

private void checkForDevices()
{
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

    while(deviceIterator.hasNext())
    {
        UsbDevice device = deviceIterator.next();

           /* if (device.getVendorId()==VID && device.getProductId()==PID)
            {
                Log.d(TAG, "Found a device: " + device);

                manager.requestPermission(device, mPermissionIntent);
            }*/

        Log.d(TAG, "Found a device: " + device);

        manager.requestPermission(device, mPermissionIntent);
    }
}

}

JNI代码:

#include <jni.h>
#include <string.h>
#include <stdio.h>

extern "C"
JNIEXPORT jstring JNICALL
Java_nrrsmdm_com_myapplication_MainActivity_ndkopenfile
        (JNIEnv *env, jobject obj)
{
    int errno = 0;
    char myStr[20];
    FILE* fp = fopen("/storage/9E6D-8A07/COMMWR","w+");
    if(fp!=NULL)
    {
        fputs("HELLO WORLD!\n", fp);
        fflush(fp);
        fclose(fp);
        return env->NewStringUTF(myStr);
    }
    else
    { sprintf("error","errno = %d",errno);
        fclose(fp);
        return env->NewStringUTF("Error opening file!");
    }

   /* std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());*/


}

1 个答案:

答案 0 :(得分:0)

尝试向您的清单添加权限:

<manifest>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    ...
    <application>
        ...
        <activity> 
            ...
        </activity>
    </application>
</manifest> 

要支持marshmallow,请确保您的活动实现OnRequestPermissionResult并添加代码以请求权限(如果尚未授予)(代码来自对此question的回答):

public boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG,"Permission is granted");
            return true;
        } else {

            Log.v(TAG,"Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v(TAG,"Permission is granted");
        return true;
    }
}

权限结果回调:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
        //resume tasks needing this permission
    }
}