首页

android 相机拍照保存

java

2020-7-23

package com.example.systemcamera;

import java.io.BufferedOutputStream;  
import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.Calendar;  
import java.util.Locale;  
  
import android.app.Activity;  
import android.content.Intent;  
import android.graphics.Bitmap;  
import android.os.Bundle;  
import android.os.Environment;  
import android.provider.MediaStore;  
import android.text.format.DateFormat;  
import android.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.ImageView;  
import android.widget.Toast;  
  
public class MainActivity extends Activity {  
    /** Called when the activity is first created. */  
    private Button button;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        button = (Button) findViewById(R.id.button1);  
        button.setOnClickListener(new OnClickListener() {  
  
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  
                startActivityForResult(intent, 1);  
            }  
        });  
    }  
  
    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        // TODO Auto-generated method stub  
        super.onActivityResult(requestCode, resultCode, data);  
        if (resultCode == Activity.RESULT_OK) {  
            String sdStatus = Environment.getExternalStorageState();  
            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用  
                Log.i("TestFile",  
                        "SD card is not avaiable/writeable right now.");  
                return;  
            }  
            String name = new DateFormat().format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA))   ".jpg";     
            Toast.makeText(this, name, Toast.LENGTH_LONG).show();  
            Bundle bundle = data.getExtras();  
            Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式  
          
            FileOutputStream b = null;  
           //???????????????????????????????为什么不能直接保存在系统相册位置呢????????????  
            File file = new File("/sdcard/myImage/");  
            file.mkdirs();// 创建文件夹  
            String fileName = "/sdcard/myImage/" name;  
  
            try {  
                b = new FileOutputStream(fileName);  
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } finally {  
                try {  
                    b.flush();  
                    b.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            ((ImageView) findViewById(R.id.imageView1)).setImageBitmap(bitmap);// 将图片显示在ImageView里  
        }  
    }  
}  

资源下载此资源下载价格为2D币(VIP免费),请先
资源文件列表
SystemCamera/.classpath , 475
SystemCamera/.project , 848
SystemCamera/.settings/org.eclipse.jdt.core.prefs , 177
SystemCamera/AndroidManifest.xml , 987
SystemCamera/bin/AndroidManifest.xml , 987
SystemCamera/bin/classes.dex , 1119380
SystemCamera/bin/classes/com/example/systemcamera/BuildConfig.class , 357
SystemCamera/bin/classes/com/example/systemcamera/MainActivity$1.class , 1040
SystemCamera/bin/classes/com/example/systemcamera/MainActivity.class , 3878
SystemCamera/bin/classes/com/example/systemcamera/R$attr.class , 358
SystemCamera/bin/classes/com/example/systemcamera/R$dimen.class , 479
SystemCamera/bin/classes/com/example/systemcamera/R$drawable.class , 425
SystemCamera/bin/classes/com/example/systemcamera/R$id.class , 509
SystemCamera/bin/classes/com/example/systemcamera/R$layout.class , 421
SystemCamera/bin/classes/com/example/systemcamera/R$menu.class , 406
SystemCamera/bin/classes/com/example/systemcamera/R$string.class , 490
SystemCamera/bin/classes/com/example/systemcamera/R$style.class , 449
SystemCamera/bin/classes/com/example/systemcamera/R.class , 733
SystemCamera/bin/dexedLibs/android-support-v4-19215f60219777fd61f86f9b2b37cd63.jar , 367658
SystemCamera/bin/resources.ap_ , 41482
SystemCamera/bin/res/crunch/drawable-hdpi/ic_launcher.png , 5964
SystemCamera/bin/res/crunch/drawable-mdpi/ic_launcher.png , 3112
SystemCamera/bin/res/crunch/drawable-xhdpi/ic_launcher.png , 9355
SystemCamera/bin/res/crunch/drawable-xxhdpi/ic_launcher.png , 17889
SystemCamera/bin/SystemCamera.apk , 415823
SystemCamera/gen/com/example/systemcamera/BuildConfig.java , 166
SystemCamera/gen/com/example/systemcamera/R.java , 2769
SystemCamera/ic_launcher-web.png , 51394
SystemCamera/libs/android-support-v4.jar , 1074044
SystemCamera/proguard-project.txt , 781
SystemCamera/project.properties , 563
SystemCamera/res/drawable-hdpi/ic_launcher.png , 7658
SystemCamera/res/drawable-mdpi/ic_launcher.png , 3777
SystemCamera/res/drawable-xhdpi/ic_launcher.png , 12516
SystemCamera/res/drawable-xxhdpi/ic_launcher.png , 24777
SystemCamera/res/layout/activity_main.xml , 1373
SystemCamera/res/menu/main.xml , 374
SystemCamera/res/values-v11/styles.xml , 334
SystemCamera/res/values-v14/styles.xml , 391
SystemCamera/res/values-w820dp/dimens.xml , 381
SystemCamera/res/values/dimens.xml , 220
SystemCamera/res/values/strings.xml , 229
SystemCamera/res/values/styles.xml , 697
SystemCamera/src/com/example/systemcamera/MainActivity.java , 3262
没有账号? 忘记密码?

社交账号快速登录