XMLRPC Wordpress图像上传 - 上传空图像?

时间:2017-08-04 21:41:15

标签: java android wordpress base64 xml-rpc

我正在尝试使用XMLRPC将图像文件上传到Wordpress网站。 我正在使用org.apache.xmlrpc:xmlrpc-client从Android进行连接,我可以成功点击网站,然后触发上传,但网站上的图像文件为空。我已经发送了一个base64编码的字符串作为WP API状态,但我无法让它工作?

任何人都知道为什么? 这是我的代码:

public static void updatePhoto(final String status, final Context context, final String uri) {

    websiteUrl = (String) loadPreference("wordpressUrl", context,String.class);
    username = (String) loadPreference("wordpressUsername", context,String.class);
    password = (String) loadPreference("wordpressPassword", context,String.class);

    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {


            try {
                XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
                config.setServerURL(new URL(websiteUrl+"/xmlrpc.php"));

                XmlRpcClient rpcClient = new XmlRpcClient();
                rpcClient.setConfig(config);

                Uri realUri = getImageContentUri(context,Uri.parse(uri).getPath());

                final InputStream imageStream = context.getContentResolver().openInputStream(realUri);
                Bitmap originalBitmap = BitmapFactory.decodeStream(imageStream);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                originalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] outputByteArray = baos.toByteArray();

                String base64EncodedString = Base64.encodeToString(outputByteArray, Base64.DEFAULT);

                Map content = new Hashtable();
                content.put("name", status+".jpg");
                content.put("type", "image/jpeg");
                content.put("bits", base64EncodedString);
                content.put("overwrite", false);


                // Make the XMLRPC call.
                Object result = rpcClient.execute("wp.uploadFile", new Object[]{
                        0,
                        username,
                        password,
                        content
                });

                // Print result - this is the new post's ID.
                System.out.println(result);

            } catch (final XmlRpcException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });

            } catch (final Exception e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
            }

        }
    });

}

public static Uri getImageContentUri(Context context, String absPath) {
    Log.v(TAG, "getImageContentUri: " + absPath);

    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI
            , new String[] { MediaStore.Images.Media._ID }
            , MediaStore.Images.Media.DATA + "=? "
            , new String[] { absPath }, null);

    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
        return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , Integer.toString(id));

    } else if (!absPath.isEmpty()) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DATA, absPath);
        return context.getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    } else {
        return null;
    }
}

private static String encodeImage(Bitmap bm)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
    byte[] b = baos.toByteArray();
    String encImage = Base64.encodeToString(b, Base64.NO_WRAP);

    return encImage;
}

0 个答案:

没有答案