Android_SmartDeviceLink_Uploading Files and Graphics

我的博客

Uploading Files and Graphics上传图片和文件

图形允许你更好地定制你想让你的用户看到和提供更好的用户界面
当使用SmartDeviceLink开发应用程序时,在使用图形时必须记住两件事:
您可以连接到一个不显示图形的头部单元。
你必须在使用之前将它们从你的移动设备上传到Core

Detecting if Graphics are Supported检查是否支持图形

能够知道图形是否被支持是您的应用程序的一个非常重要的特性,因为这避免了将不必要的图像上传到head单元。为了查看是否支持图形,请使用有效SdlProxyALM 的getDisplayCapabilities()方法来查找头部单元的显示功能。

Uploading a File using PutFile

要将文件上传到Corn,请使用PutFile RPC。您可以使用RPC响应回调来对成功或失败的上传结果执行某些操作。这里有一个例子,尝试上传一个图像到Corn,在成功上传后,设置app的HMI图标。

PutFile putFileRequest = new PutFile();
putFileRequest.setSdlFileName("appIcon.jpeg");
putFileRequest.setFileType(FileType.GRAPHIC_JPEG);
putFileRequest.setPersistentFile(true);
putFileRequest.setFileData(file_data); // can create file_data using helper method below
putFileRequest.setCorrelationID(CorrelationIdGenerator.generateId());
putFileRequest.setOnRPCResponseListener(new OnRPCResponseListener() {
 
    @Override
    public void onResponse(int correlationId, RPCResponse response) {
        setListenerType(UPDATE_LISTENER_TYPE_PUT_FILE); // necessary for PutFile requests
 
        if(response.getSuccess()){
            try {
                proxy.setappicon("appIcon.jpeg", CorrelationIdGenerator.generateId());
            } catch (SdlException e) {
                e.printStackTrace();
            }
        }else{
            Log.i("SdlService", "Unsuccessful app icon upload.");
        }
    }
});
try {
    proxy.sendRPCRequest(putFileRequest);
} catch (SdlException e) {
    e.printStackTrace();
}

应用程序图标和其他图像可能会硬编码到应用程序中,获取原始数据将非常重要。下面的方法将帮助获取该数据

/**
* Helper method to take resource files and turn them into byte arrays
* @param resource Resource file id.
* @return Resulting byte array.
*/
private byte[] contentsOfResource(int resource) {
    InputStream is = null;
    try {
        is = getResources().openRawResource(resource);
        ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());
        final int bufferSize = 4096;
        final byte[] buffer = new byte[bufferSize];
        int available;
        while ((available = is.read(buffer)) >= 0) {
            os.write(buffer, 0, available);
        }
        return os.toByteArray();
    } catch (IOException e) {
        Log.w(TAG, "Can't read icon file", e);
        return null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

File Naming:文件命名

文件名称只能由字母(a-Z)和数字(0-9)组成,否则SDL核心可能无法找到上传的文件(即使上传成功)。

File Persistance文件持久化

PutFile支持上传持久图像,即当应用程序断开连接时不会被删除的图像。持久化应该用于和UI相关的图形,而不是动态的方面。

putFileRequest.setPersistentFile(true);

注意:需要知道:如果头部单元的空间是有限的,持久化就行不通了

Overwrite Stored Files

如果上传的文件与已经上传的文件同名,新文件将覆盖前面的文件。

Check if a File Has Already Been Uploaded检查文件是否已经上传

您可以使用ListFiles RPC检查上传文件的名称,以避免覆盖文件。

ListFiles listFiles = new ListFiles();
listFiles.setCorrelationID(CorrelationIdGenerator.generateId());
listFiles.setOnRPCResponseListener(new OnRPCResponseListener() {
    @Override
    public void onResponse(int correlationId, RPCResponse response) {
        if(response.getSuccess()){
            List<String> filenames = ((ListFilesResponse) response).getFilenames();
            if(filenames.contains("appIcon.jpeg")){
                Log.i("SdlService", "App icon is already uploaded.");
            }else{
                Log.i("SdlService", "App icon has not been uploaded.");
            }
        }else{
            Log.i("SdlService", "Failed to request list of uploaded files.");
        }
    }
});
try{
    proxy.sendRPCRequest(listFiles);
} catch (SdlException e) {
    e.printStackTrace();
}

Check the Amount of File Storage(检查文件存储的数量)

要查找在头部单元上留下的文件存储量,可以使用类似的方式使用ListFiles RPC。

ListFiles listFiles = new ListFiles();
listFiles.setCorrelationID(CorrelationIdGenerator.generateId());
listFiles.setOnRPCResponseListener(new OnRPCResponseListener() {
    @Override
    public void onResponse(int correlationId, RPCResponse response) {
        if(response.getSuccess()){
            Integer spaceAvailable = ((ListFilesResponse) response).getSpaceAvailable();
            Log.i("SdlService", "Space available on Core = " + spaceAvailable);
        }else{
            Log.i("SdlService", "Failed to request list of uploaded files.");
        }
    }
});
try{
    proxy.sendRPCRequest(listFiles);
} catch (SdlException e) {
    e.printStackTrace();
}

Image Specifics图片细节

Image File Type

图像可以格式化为PNG、JPEG或BMP。检查您的SdlProxyALM提供的display功能对象,以了解head单元支持的图像格式。

Image Sizes

如果上传的图像大于支持的大小,那么图像将按比例缩小以容纳。

IMAGE SPECIFICATIONS

《Android_SmartDeviceLink_Uploading Files and Graphics》 image.png

    原文作者:勇敢写信
    原文地址: https://www.jianshu.com/p/50600bf206c8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞