日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

简单的 Android 拍照并显示以及获取路径后上传

發(fā)布時間:2024/1/18 Android 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单的 Android 拍照并显示以及获取路径后上传 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

簡單的 Android 拍照并顯示以及獲取路徑后上傳

Activity 中的代碼,我只貼出重要的事件部分代碼

public void doPhoto(View view){destoryBimap();String state = Environment.getExternalStorageState();if (state.equals(Environment.MEDIA_MOUNTED)) {Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");startActivityForResult(intent, 1);} else {Toast.makeText(MainActivity.this, "沒有SD卡", Toast.LENGTH_LONG).show();}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data){Uri uri = data.getData();if (uri != null) {this.photo = BitmapFactory.decodeFile(uri.getPath());}if (this.photo == null) {Bundle bundle = data.getExtras();if (bundle != null) {this.photo = (Bitmap) bundle.get("data");} else {Toast.makeText(MainActivity.this, "拍照失敗", Toast.LENGTH_LONG).show();return;}}FileOutputStream fileOutputStream = null;try {// 獲取 SD 卡根目錄String saveDir = Environment.getExternalStorageDirectory() + "/meitian_photos";// 新建目錄File dir = new File(saveDir);if (! dir.exists()) dir.mkdir();// 生成文件名SimpleDateFormat t = new SimpleDateFormat("yyyyMMddssSSS");String filename = "MT" + (t.format(new Date())) + ".jpg";// 新建文件File file = new File(saveDir, filename);// 打開文件輸出流fileOutputStream = new FileOutputStream(file);// 生成圖片文件this.photo.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);// 相片的完整路徑this.picPath = file.getPath();ImageView imageView = (ImageView) findViewById(R.id.showPhoto);imageView.setImageBitmap(this.photo);} catch (Exception e) {e.printStackTrace();} finally {if (fileOutputStream != null) {try {fileOutputStream.close();} catch (Exception e) {e.printStackTrace();}}}}/*** 銷毀圖片文件*/private void destoryBimap(){if (photo != null && ! photo.isRecycled()) {photo.recycle();photo = null;}}


Layout 布局頁面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><ScrollViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><Buttonandroid:id="@+id/doPhoto"android:layout_width="fill_parent"android:layout_height="wrap_content"android:padding="10dp"android:layout_marginBottom="10dp"android:text="拍照"android:onClick="doPhoto"/><TextViewandroid:id="@+id/showContent"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dp"/><ImageViewandroid:id="@+id/showPhoto"android:layout_width="fill_parent"android:layout_height="250dp"android:scaleType="centerCrop"android:src="@drawable/add"android:layout_marginBottom="10dp"/></LinearLayout></ScrollView> </LinearLayout>
其中的上傳工具類請查看該文章:

http://blog.csdn.net/zhouzme/article/details/18952053




轉載于:https://www.cnblogs.com/zhouzme/p/5758512.html

總結

以上是生活随笔為你收集整理的简单的 Android 拍照并显示以及获取路径后上传的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。