Android가 카메라를 호출하여 사진을 sd카드에 저장하는 방법
 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, 1); onActivityResult(int requestCode, int resultCode, Intent data) Bitmap 。Bitmap bitmap = (Bitmap) data.getExtras().get("data"); 
 
String sdStatus = Environment.getExternalStorageState(); 
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { //  sd  
Log.v("TestFile", 
"SD card is not avaiable/writeable right now."); 
return; 
} 
 
File file = new File("/sdcard/myImage/"); 
file.mkdirs();//   
String fileName = "/sdcard/myImage/111.jpg"; 
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(); 
} 
} 
 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 
 
package barry.android.c; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
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.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
public class MyCaremaActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
Button button = (Button) findViewById(R.id.button); 
button.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, 1); 
} 
}); 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
if (resultCode == Activity.RESULT_OK) { 
String sdStatus = Environment.getExternalStorageState(); 
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { //  sd  
Log.v("TestFile", 
"SD card is not avaiable/writeable right now."); 
return; 
} 
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/111.jpg"; 
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.imageView)).setImageBitmap(bitmap);//  ImageView  
} 
} 
} 
 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<Button 
android:id="@+id/button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text=" " /> 
<ImageView 
android:id="@+id/imageView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#999999" /> 
</LinearLayout> 
 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="barry.android.c" 
android:versionCode="1" 
android:versionName="1.0" > 
<uses-sdk android:minSdkVersion="7" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 
<application 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" > 
<activity 
android:label="@string/app_name" 
android:name=".MyCaremaActivity" > 
<intent-filter > 
<action android:name="android.intent.action.MAIN" /> 
<category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 
</activity> 
</application> 
</manifest> 
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.