1. 程式人生 > >android 新增視訊、圖片、錄音上傳(一)

android 新增視訊、圖片、錄音上傳(一)

實現一個功能,上報資訊,可以新增視訊、圖片、錄音。呼叫系統相機,所以每次只能選擇一張圖片。

    //拍照
        add_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (commitId.equals("0")) {
                    Toast.makeText(getApplicationContext(), "請先儲存上面的資訊"
, 0).show(); return; }else{ Intent local = new Intent(); local.setType("image/*"); local.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(local, 11); } } }); //錄影
add_video.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (commitId.equals("0")) { Toast.makeText(getApplicationContext(), "請先儲存上面的資訊", 0).show(); return; }else
{ Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); startActivityForResult(intent, 10); } } });

拍照和視訊的處理

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(resultCode== Activity.RESULT_OK){    
            switch(requestCode) {
            case 10:
                Uri uriVideo = data.getData();    
                Cursor cursors=this.getContentResolver().query(uriVideo, null, null, null, null);    
                if (cursors.moveToNext()) {    
                        /* _data:檔案的絕對路徑 ,_display_name:檔名 */    
                    videoPath = cursors.getString(cursors.getColumnIndex("_data"));    
                        Toast.makeText(this, videoPath, Toast.LENGTH_SHORT).show(); 
                        //儲存視訊路徑
                        imagelist.add(videoPath);
                        setImageGrideValue();   
                }    
                break;
            case 11:
                Uri uri = data.getData();
                Log.e(TAG, "uri = " + uri);
                try {
                    String[] pojo = { MediaStore.Images.Media.DATA };

                    Cursor cursor = managedQuery(uri, pojo, null, null, null);
                    if (cursor != null) {
                        ContentResolver cr = this.getContentResolver();
                        int colunm_index = cursor
                                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        cursor.moveToFirst();
                        String path = cursor.getString(colunm_index);
                        /***
                         * 這裡加這樣一個判斷主要是為了第三方的軟體選擇,比如:使用第三方的檔案管理器的話,你選擇的檔案就不一定是圖片了,
                         * 這樣的話,我們判斷檔案的字尾名 如果是圖片格式的話,那麼才可以
                         */
                        if (path.endsWith("jpg") || path.endsWith("png")) {
                            srcPath=path;
                            //儲存圖片路徑
                            imagelist.add(srcPath);
                            setImageGrideValue();
                        } else {
                            alert();
                        }
                    } else {
                        alert();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
                }
            }
        super.onActivityResult(requestCode, resultCode, data);
    }

     private void alert() {
            Dialog dialog = new AlertDialog.Builder(this).setTitle("提示")
                    .setMessage("您選擇的不是有效的圖片")
                    .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            srcPath = null;
                        }
                    }).create();
            dialog.show();
        }

在頁面顯示新增的視訊和照片列表(視訊使用的是預設圖片顯示)

    @AbIocView(id=R.id.gridView_image_report)
    GridView gridView_image_report;
    private ArrayList<String> imagelist=new ArrayList<String>();

 private void setImageGrideValue() {
        setImageListener();
        MAImagedapter mAdapter = new MAImagedapter(context);
        gridView_image_report.setAdapter(mAdapter);
        LayoutParams params = new LayoutParams(mAdapter.getCount() * (200 + 10),
                LayoutParams.WRAP_CONTENT);
        gridView_image_report.setLayoutParams(params);
        gridView_image_report.setColumnWidth(200);
        gridView_image_report.setHorizontalSpacing(10);
        gridView_image_report.setStretchMode(GridView.NO_STRETCH);
        gridView_image_report.setNumColumns(mAdapter.getCount());

    }

    private void setImageListener() {
        gridView_image_report.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Log.e(TAG, "position = " + position);
                String url=imagelist.get(position);
//                Intent intent = new Intent(AddZhiliangReportActivity.this, PicturePreviewActivity.class);  
//              intent.putExtra("url", url);
//              startActivity(intent);
            }

        });
        //長按刪除
        gridView_image_report.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                imagelist.remove(arg2);
                setImageGrideValue();
                return false;
            }
        });
    }

    class MAImagedapter extends BaseAdapter {
         Context mContext;
         LayoutInflater mInflater;

         public MAImagedapter(Context c) {
             mContext = c;
             mInflater = LayoutInflater.from(mContext);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return imagelist.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int position, View contentView, ViewGroup arg2) {
            String image=imagelist.get(position);
            //視訊預設圖片
            if (image.contains("3gp")||image.contains("mp4")) {
                image=URLs.URL_API_HOST+"public/images/video_play_btn.png";
            }
            Log.e(TAG, image);
            ViewHolder holder;
            if (contentView == null) {
                holder = new ViewHolder();
                contentView = mInflater.inflate(R.layout.gridview_item, null);
                holder.mImg = (ImageView) contentView.findViewById(R.id.mImage);
                Bitmap bitmap=ImageLoader.getInstance().loadImage(image, 200, 100, new OnCallBackListener() {
                    @Override
                    public void setOnCallBackListener(Bitmap bitmap, String url) {
                        ImageView image=(ImageView) gridView_image_report.findViewWithTag(url);
                        if(image!=null&&bitmap!=null){
                            image.setImageBitmap(bitmap);
                        }
                    }
                });
                if(bitmap!=null){
                    holder.mImg.setImageBitmap(bitmap);
                }else{              
                    holder.mImg.setImageResource(R.drawable.friends_sends_pictures_no);
                }
            } else {
                holder = (ViewHolder) contentView.getTag();
            }
            contentView.setTag(holder);
            return contentView;
        }

    }
static class ViewHolder {
            ImageView mImg;
        }

gridview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/widget29"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:id="@+id/mImage"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/image_loading" >

</ImageView>
</RelativeLayout>
<LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <HorizontalScrollView
                    android:id="@+id/horizontalScrollView_report"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFF"
                    android:orientation="horizontal" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal" >

                        <GridView
                            android:id="@+id/gridView_image_report"
                            android:layout_width="match_parent"
                            android:layout_height="60dp" >
                        </GridView>
                    </LinearLayout>
                </HorizontalScrollView>
            </LinearLayout>