导航:首页 > 编程语言 > 安卓开发图片上传代码

安卓开发图片上传代码

发布时间:2024-12-15 19:24:33

1. 安卓如何在后台上传图片等资料

public class EX08_11 extends Activity
{
/* 变量声明
* newName:上传后在服务器上的文件名称
* uploadFile:要上传的文件路径
* actionUrl:服务器对应的程序路径 */
// private String newName="345444.jpg";
private String uploadFile="/sdcard/345444.jpg";
private String actionUrl="http://*********/upload.PHP";
private TextView mText1;
private TextView mText2;
private Button mButton;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mText1 = (TextView) findViewById(R.id.myText2);
mText1.setText("文件路径:/n"+uploadFile);

mText2 = (TextView) findViewById(R.id.myText3);
mText2.setText("上传网址:/n"+actionUrl);
/* 设定mButton的onClick事件处理 */
mButton = (Button) findViewById(R.id.myButton);
mButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
uploadFile();
}
});
}

/* 上传文件吹Server的method */
private void uploadFile()
{
// String end = "/r/n";
// String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
// con.setReadTimeout(5 * 1000);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设定传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("enctype",
"multipart/form-data;boundary="+boundary);
/* 设定DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
/*ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=/"file1/";filename=/"" +
newName +"/"" + end);
ds.writeBytes(end); */

/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设定每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

int length = -1;
/* 从文件读取数据到缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将数据写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
// ds.writeBytes(end);
// ds.writeBytes(twoHyphens + boundary + twoHyphens + end);

/* close streams */
fStream.close();
ds.flush();

/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 将Response显示于Dialog */
showDialog(b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog(""+e);
}
}

/* 显示Dialog的method */
private void showDialog(String mess)
{
new AlertDialog.Builder(EX08_11.this).setTitle("Message")
.setMessage(mess)
.setNegativeButton("确定",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
})
.show();
}
}

PHP代码

[php] view plain
$data = file_get_contents('php://input');
$handle = fopen($_SERVER['DOCUMENT_ROOT'].'/image/345444.jpg', 'w');
if ($handle)
{
fwrite($handle,$data);
fclose($handle);
echo "success";
}

2. 使用android上传图片到服务器,并且把图片保存到服务器的某个文件夹里

有两种方法,第一,把你的图片转成字节流,然后用post方法把字节流传到服务端,然后服务端接收到字节流之后,开启一个线程把它重新压缩成图片,保存在某个文件夹下面。
第二,开启一个线程,用socket直接把图片放到stream中传到服务端,服务端接收后保存到文件夹下。

3. Android图片批量上传的功能。(图片比较大)

Android中上传图片或者下载图片,使用最多的是xUtils和imageloader、glide,选用这两种的哪一种框架都行,因为是批量和回图片大容易造成界面卡以答及上传速度慢,对图片操作不当就容易造成OOM异常,一般对于批量上传大图片都需要对图片也处理,然后在上传第一步需要对图片进行比例压缩之后再进行质量压缩,处理之后的图片比之前的图片会小很多,再加上框架的上传处理,会有很好的效果,希望对你有所帮助

阅读全文

与安卓开发图片上传代码相关的资料

热点内容
监控网络带宽测算 浏览:542
网络ip电话怎么使用 浏览:379
怎么用cad编数控火焰切割编程 浏览:969
有什么app可以一起看电视剧的 浏览:539
什么app有几万人的群 浏览:101
哪个小说app看辰东小说 浏览:664
qq有种红包y上两点 浏览:798
没学历的怎么学编程 浏览:906
无线桥接网络不稳定如何解决 浏览:284
孩子要学编程怎么办 浏览:635
matlab调用c程序心得 浏览:921
史记台湾版本 浏览:397
iphone文件可以存到u盘吗 浏览:327
肖战家中直播用的什么app 浏览:2
spring下载文件名乱码 浏览:575
有哪些下载大学课件的网站 浏览:615
linux怎样删除非空文件 浏览:628
网络电台如何实时播放 浏览:660
职业技术学院学编程怎么样 浏览:569
按键精灵哪个版本好用 浏览:896

友情链接