当前位置:  编程技术>移动开发
本页文章导读:
    ▪byte,drawable,bit地图图片相互转换        byte,drawable,bitmap图片相互转换 private Drawable Bytes2Drawable(byte[] b) { if (b.length != 0) { Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length); Drawable drawable = new BitmapDrawable(bitmap); return drawable; } else .........
    ▪ URL 文件下传的原理        URL 文件上传的原理 <?php function upload_curl_pic() { $url  = 'http://localhost//5-5-5//uploadfile.php';  //target url $file = 'c:/21.jpg'; //要上传的文件 $fields['f'] = '@'.$file; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ur.........
    ▪ NSLog 加强       NSLog 增强 #ifdef DEBUG #   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #else #   define DLog(...) #endif   // ALog always displays output regardless of the DEBUG setting #define ALog(fmt, .........

[1]byte,drawable,bit地图图片相互转换
    来源: 互联网  发布时间: 2014-02-18
byte,drawable,bitmap图片相互转换
private Drawable Bytes2Drawable(byte[] b) {
if (b.length != 0) {
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
Drawable drawable = new BitmapDrawable(bitmap);
return drawable;
} else {
return null;
}
}


private byte[] drawableToByte(int position){
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
mThumbIds[position]);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}


private byte[] bitmapToByte(Bitmap bm) {
Bitmap bmp = bm;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos);
return baos.toByteArray();
}

    
[2] URL 文件下传的原理
    来源: 互联网  发布时间: 2014-02-18
URL 文件上传的原理

<?php
function upload_curl_pic()
{
$url  = 'http://localhost//5-5-5//uploadfile.php';  //target url
$file = 'c:/21.jpg'; //要上传的文件
$fields['f'] = '@'.$file;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );
curl_exec( $ch );
if ($error = curl_error($ch) ) {
       die($error);
}
curl_close($ch); 
}
upload_curl_pic();//可以扩展把图片地址传到函数中
?>


接收端:
<?php
$uploaddir = 'E:\\wwwroot\\www\\htdocs\\5-5-5\\pic_all_here\\';
$uploadfile = $uploaddir . $_FILES['f']['name'];
if (move_uploaded_file($_FILES['f']['tmp_name'], $uploadfile))

    echo "File is valid, and was successfully uploaded.\n";
} else {
            echo "Possible file upload attack!\n";
            echo 'Here is some more debugging info:';
         

       }

?>

android上面图片的上传可以用apache包里面的httpclient和MultipartEntity来上传图片,这种的上传方式的话由于都封装好了所以看不到HTTP协议里面具体是怎样上传的;
其实图片的上传还可以用Java自带的HttpURLConnection来做上传处理,例如有一个PHP写的接收图片的POST接口http://localhost/upload/upload.php
img[] 图片(支持多张图片上传)

<?php
    echo $_POST["name"];
    echo $_POST["address"];
                                                                  
    $img = $_FILES['img'];
    if ($img){
        //文件存放目录,和本php文件同级
        $dir = dirname(__file__);
        $i = 0;
        foreach ($img['tmp_name'] as $value){
            $filename = $img['name'][$i];
            if ($value){
                $savepath="$dir\\$filename";
                $state = move_uploaded_file($value, $savepath);
                //如果上传成功,预览
                if($state){
                    echo $filename." upload success";
                }
            }
            $i++;
        }
    }
?>


这时假如我们上传的参数为:
name值为this is the parameter:name
address值为this is the parameter:address
img[]的值为两张图片,路径为E:\Photos\test_1.jpg 和E:\Photos\test_2.jpg
那么我们写的POST网络请求的方法体应该为下面的格式:
-----------------------------7dc2fd5c0894
Content-Disposition: form-data; name="name"
this is the parameter:name
-----------------------------7dc2fd5c0894
Content-Disposition: form-data; name="address"
this is the parameter:address
-----------------------------7dc2fd5c0894
Content-Disposition: form-data; name="img[]"; filename="E:\Photos\test_1.jpg"
Content-Type: image/pjpeg

<图片E:\Photos\test_1.jpg的二进制数据未显示>
---------------------------7dc2fd5c0894
Content-Disposition: form-data; name="img[]"; filename="E:\Photos\test_2.jpg"
Content-Type: image/pjpeg
<图片E:\Photos\test_2.jpg的二进制数据未显示 >
-----------------------------7dc2fd5c0894--

根据上面的格式我们写的上传图片的Java代码如下:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
                                    
public class Main {
                                    
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("E:\\Photos\\test_1.jpg");//要上传的文件路径
        list.add("E:\\Photos\\test_2.jpg");//要上传的文件路径
                                    
        upload("this is the parameter:name", "this is the parameter:address",
                list);
    }
                                    
    public static void upload(String name, String address, List<String> list) {
                                    
        try {
            // 定义数据分隔线
            String BOUNDARY = "------------------------7dc2fd5c0894";
            // 定义最后数据分隔线
            byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
                                    
            URL url = new URL("http://localhost/upload/upload.php");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                                    
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)");
            conn.setRequestProperty("Charsert", "UTF-8");
            conn.setRequestProperty("Content-Type",
                    "multipart/form-data; boundary=" + BOUNDARY);
                                    
            OutputStream out = new DataOutputStream(conn.getOutputStream());
                                    
            // name参数
            StringBuffer params = new StringBuffer();
            params.append("--" + BOUNDARY + "\r\n");
            params.append("Content-Disposition: form-data; name=\"name\"\r\n\r\n");
            params.append(name);
            params.append("\r\n");
                                    
            // address参数
            params.append("--" + BOUNDARY + "\r\n");
            params.append("Content-Disposition: form-data; name=\"address\"\r\n\r\n");
            params.append(address);
            params.append("\r\n");
                                    
            out.write(params.toString().getBytes());
                                    
            int leng = list.size();
            for (int i = 0; i < leng; i++) {
                String fname = list.get(i);
                File file = new File(fname);
                                    
                StringBuilder sb = new StringBuilder();
                sb.append("--");
                sb.append(BOUNDARY);
                sb.append("\r\n");
                sb.append("Content-Disposition: form-data;name=\"img[]\";filename=\""
                        + file.getName() + "\"\r\n");
                // 这里不能漏掉,根据文件类型来来做处理,由于上传的是图片,所以这里可以写成image/pjpeg
                sb.append("Content-Type:image/pjpeg\r\n\r\n");
                out.write(sb.toString().getBytes());
                                    
                DataInputStream in = new DataInputStream(new FileInputStream(
                        file));
                int bytes = 0;
                byte[] bufferOut = new byte[1024];
                while ((bytes = in.read(bufferOut)) != -1) {
                    out.write(bufferOut, 0, bytes);
                }
                out.write("\r\n".getBytes());
                in.close();
            }
            out.write(end_data);
            out.flush();
            out.close();
                                    
            // 定义BufferedReader输入流来读取URL的响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
                                    
        } catch (Exception e) {
            System.out.println("发送POST请求出现异常!" + e);
            e.printStackTrace();
        }
    }
                                    
}
这样就可以把多张图片和参数上传到服务器了。当然这里只是一个演示,是用php写的接口并只是简单的把图图片保存在了接口文件的当前文件夹下;另外图片也没有做压缩,android手机上面上传时一般会做压缩,而且也不会一个接口上传多张图片,网络差时给用户的体验很不好,但是对于互联网开发者来说深入理解HTTP协议对表单是怎样处理的这个还是很重要


    
[3] NSLog 加强
    来源: 互联网  发布时间: 2014-02-18
NSLog 增强

#ifdef DEBUG

#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);

#else

#   define DLog(...)

#endif

 

// ALog always displays output regardless of the DEBUG setting

#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);

 

 

 

使用时,  替换NSLog,使用  DLog或ALog


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3