android添加图片资源文件,android文件上传示例分享(android图片上传)

package com.guotop.elearn.activity.app.yunpan.item;

import java.io.File;

import java.lang.ref.WeakReference;

import java.util.Random;

import org.json.JSONException;

import org.json.JSONObject;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.os.IBinder;

import android.os.Looper;

import android.os.Message;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.ProgressBar;

import android.widget.TextView;

import android.widget.Toast;

import com.guotop.base.L;

import com.guotop.base.handler.BaseHandler;

import com.guotop.base.thread.HttpThread;

import com.guotop.base.util.MyFile;

import com.guotop.base.util.MyHashMap;

import com.guotop.elearn.activity.R;

import com.guotop.elearn.activity.app.yunpan.Y;

import com.guotop.elearn.activity.app.yunpan.bean.UploadYunFileInformaction;

import com.guotop.elearn.activity.app.yunpan.thread.UploadYunFileHttpThread;

/**

*

*

* @author: 李杨

* @time: 2014-4-21下午12:28:33

*/

public class YunPanUploadFileItem implements OnClickListener {

LinearLayout view,parentView;

String filePath;

private Context context;

private TextView uploadFileProgressText, uploadFileName;

private ProgressBar uploadFileProgressBar;

private ImageView uploadFileImg;

private Button startUploadFileBtn, cancelUploadFileBtn;

private String actionURL;

BaseHandler handler;

UploadYunFileHttpThread t;

UploadYunFileInformaction uploadYunFileInformaction ;

public YunPanUploadFileItem(Context context,LinearLayout parentView, String filePath,String actionURL) {

this.parentView = parentView;

this.actionURL = actionURL;

this.context = context;

File file = new File(filePath);

this.view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.activity_yunpan_uploadfile_item, null);

//        this.view = view;

this.filePath = filePath;

uploadFileProgressText = (TextView) view.findViewById(R.id.uploadFileProgressText);

uploadFileName = (TextView) view.findViewById(R.id.uploadFileName);

uploadFileProgressBar = (ProgressBar) view.findViewById(R.id.uploadFileProgressBar);

uploadFileImg = (ImageView) view.findViewById(R.id.uploadFileImg);

cancelUploadFileBtn = (Button) view.findViewById(R.id.cancelUploadFileBtn);

startUploadFileBtn = (Button) view.findViewById(R.id.startUploadFileBtn);

uploadFileName.setText(file.getName()+"   大小"+MyFile.formetFileSize(file.getPath()));

uploadFileImg.setImageResource(MyFile.getFileIcon(file));

startUploadFileBtn.setOnClickListener(this);

cancelUploadFileBtn.setOnClickListener(this);

parentView.addView(view);

uploadYunFileInformaction = new UploadYunFileInformaction(filePath);

myHandler = new MyHandler(Looper.myLooper(), this);

uploadYunFileInformaction.setNotificationId(new Random().nextInt(10000));

uploadYunFileInformaction.setActionURL(actionURL);

t = new UploadYunFileHttpThread(myHandler, uploadYunFileInformaction);

uploads.put(uploadYunFileInformaction.getNotificationId(), t);

}

@Override

public void onClick(View v) {

if (v.getId() == R.id.startUploadFileBtn) {

downFile(t);

startUploadFileBtn.setClickable(false);

}else if(v.getId()==R.id.cancelUploadFileBtn){

if(t.isStart){

new AlertDialog.Builder(context).setTitle("系统提示!").setMessage("该文件正在上传确定要强制停止?")

.setNegativeButton("取消", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

}

}).setPositiveButton("确定", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

t.interrupt();

parentView.removeView(view);

uploads.removeKey(uploadYunFileInformaction.getNotificationId());

System.gc();

}

}).show();

}else {

parentView.removeView(view);

uploads.removeKey(uploadYunFileInformaction.getNotificationId());

}

}

}

public static MyHashMap uploads = new MyHashMap();

private MyHandler myHandler;

public IBinder onBind(Intent intent) {

return null;

}

// 下载更新文件

private void downFile(UploadYunFileHttpThread t) {

int len = 3;

if (t != null && uploads.size() <= len) {

if (!t.isStart) {

t.start();

}

} else if (t == null && uploads.size() >= len) {

t = uploads.get(len - 1);

if (!t.isStart) {

t.start();

}

}

}

/* 事件处理类 */

class MyHandler extends BaseHandler {

private WeakReference bdfs;

public MyHandler(Looper looper, YunPanUploadFileItem yunPanUploadFileItem) {

super(looper);

this.bdfs = new WeakReference(yunPanUploadFileItem);

}

@Override

public void handleMessage(Message msg) {

YunPanUploadFileItem bdfs = this.bdfs.get();

if (bdfs == null) {

return;

}

if (msg != null) {

switch (msg.what) {

case 0:

Toast.makeText(L.livingActivity, msg.obj.toString(), Toast.LENGTH_SHORT).show();

break;

case L.dowloadStart:

break;

case L.dowloadFinish:

// 下载完成后清除所有下载信息,执行安装提示

try {

uploads.removeKey(msg.getData().getInt("notificationId"));

bdfs.uploadFileProgressText.setText("上传完成");

bdfs.uploadFileProgressBar.setMax(100);

bdfs.uploadFileProgressBar.setProgress(100);

startUploadFileBtn.setClickable(false);

} catch (Exception e) {

}

bdfs.downFile(null);

break;

case L.dowloadPercentage:

// 更新状态栏上的下载进度信息

bdfs.uploadFileProgressText.setText("总共:"+MyFile.formetFileSize(msg.getData().getInt("fileSize"))+ "/" + MyFile.formetFileSize(msg.getData().getInt("finishFileSize")) + "  已上传"

+ msg.getData().getInt("percentage") + "%");

bdfs.uploadFileProgressBar.setMax(100);

bdfs.uploadFileProgressBar.setProgress(msg.getData().getInt("percentage"));

break;

case 4:

// bdfs.nm.cancel(msg.getData().getInt("notificationId"));

break;

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值