没有合适的资源?快使用搜索试试~ 我知道了~
android文件上传示例分享(android图片上传)
4 下载量 42 浏览量
2020-09-04
11:57:55
上传
评论 2
收藏 184KB PDF 举报
温馨提示
试读
16页
最近在做项目是用到Android上传功能,就写了个Android上传组件,支持拍照录用录视频和选择文件,需要的朋友可以参考下
资源详情
资源评论
资源推荐
android文件上传示例分享文件上传示例分享(android图片上传图片上传)
最近在做项目是用到Android上传功能,就写了个Android上传组件,支持拍照录用录视频和选择文件,需要的朋友可以参考
下
主要思路是调用系统文件管理器或者其他媒体采集资源来获取要上传的文件,然后将文件的上传进度实时展示到进度条中。
主Activity
复制代码 代码如下:
package com.guotop.elearn.activity.app.yunpan.activity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import com.guotop.base.activity.BaseActivity;
import com.guotop.base.util.MyFile;
import com.guotop.elearn.activity.R;
import com.guotop.elearn.activity.app.yunpan.item.YunPanUploadFileItem;
/**
*
*
* @author: 李杨
* @time: 2014-4-15下午4:29:35
*/
public class YunPanUploadFileActivity extends BaseActivity implements OnClickListener{
String userId, parentId;
private final static int FILECHOOSER_RESULTCODE = 0;
// private String openFileType="";
private String mVideoFilePath,mPhotoFilePath,mVoiceFilePath;
private Button chooseBtn,uploadBtn;
private LinearLayout conterLayout;
private String actionURL;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState, this);
setTitle("云盘上传文件");
setContentView(R.layout.activity_yunpan_uploadfile);
userId = getIntent().getStringExtra("userId");
parentId = getIntent().getStringExtra("parentId");
actionURL = getIntent().getStringExtra("actionURL");
chooseBtn = (Button)findViewById(R.id.chooseBtn);
uploadBtn = (Button)findViewById(R.id.uploadBtn);
conterLayout = (LinearLayout)findViewById(R.id.conterLayout);
chooseBtn.setOnClickListener(this);
uploadBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.chooseBtn){
// //选择文件
startActivityForResult(createDefaultOpenableIntent(), YunPanUploadFileActivity.FILECHOOSER_RESULTCODE);
// Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
// startActivityForResult(intent, YunPanUploadFileActivity.FILECHOOSER_RESULTCODE);
// Intent intent = new Intent(Media.RECORD_SOUND_ACTION);
// ((Activity) context).startActivityForResult(intent, YunPanUploadFileActivity.FILECHOOSER_RESULTCODE);
}else if(v.getId()==R.id.uploadBtn){
//上传文件
}
}
/**
* 创建上传文件
*/
public void createUploadFileItem(String filePath){
// View view = LayoutInflater.from(context).inflate(R.layout.activity_yunpan_uploadfile_item, null);
new YunPanUploadFileItem(context, conterLayout, filePath,actionURL);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
/**选择文件*/
private Intent createDefaultOpenableIntent() {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.putExtra(Intent.EXTRA_TITLE, "选择文件");
i.setType("*/*");
Intent chooser = createChooserIntent(createCameraIntent(), createCamcorderIntent(), createSoundRecorderIntent());
chooser.putExtra(Intent.EXTRA_INTENT, i);
return chooser;
}
private Intent createChooserIntent(Intent... intents) {
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents);
chooser.putExtra(Intent.EXTRA_TITLE, "选择文件");
return chooser;
}
private Intent createCameraIntent() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File externalDataDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath() + File.separator + "e-photos");
cameraDataDir.mkdirs();
mPhotoFilePath = cameraDataDir.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg";
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mPhotoFilePath)));
return cameraIntent;
}
private Intent createCamcorderIntent() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
File externalDataDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath() + File.separator + "e-videos");
cameraDataDir.mkdirs();
mVideoFilePath = cameraDataDir.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".3gp";
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mVideoFilePath)));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video
return intent;
}
private Intent createSoundRecorderIntent() {
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
File externalDataDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File cameraDataDir = new File(externalDataDir.getAbsolutePath() + File.separator + "e-voices");
cameraDataDir.mkdirs();
mVoiceFilePath = cameraDataDir.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".amr";
return intent;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILECHOOSER_RESULTCODE) {
Uri result= data == null || resultCode != RESULT_OK ? null :data.getData();
if (result == null && data == null && resultCode == Activity.RESULT_OK) {
File mMediaFile = null;;
if(new File(mVideoFilePath).exists()){
mMediaFile = new File(mVideoFilePath);
}else if(new File(mPhotoFilePath).exists()){
mMediaFile = new File(mPhotoFilePath);
}else if(new File(mVoiceFilePath).exists()){
mMediaFile = new File(mVoiceFilePath);
}
if (mMediaFile!=null&&mMediaFile.exists()) {
result = Uri.fromFile(mMediaFile);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
}
// result = Uri.fromFile(new File(mCameraFilePath));
}
if(result!=null){
if(!new File(result.getPath()).canRead()){
try {
MyFile.copyFile(new File(mVoiceFilePath),getContentResolver().openInputStream(result));
createUploadFileItem(mVoiceFilePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}else {
createUploadFileItem(result.getPath());
}
}
System.out.println(result);
}
}
}
绘制现在文件信息后的Item
复制代码 代码如下:
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;
剩余15页未读,继续阅读
weixin_38683562
- 粉丝: 6
- 资源: 970
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0