使用相机拍照并将其保存到图库
Taking a photo with Camera and saving it to Gallery
我正在尝试用相机拍照并将其保存到图库中。
这是我到目前为止所做的:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Main5Activity.this,
new String[]{android.Manifest.permission.CAMERA}, RequestCameraPermissionID);
}
}
if (ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Main5Activity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERM_WRITE_STORAGE);
} else {
takePhoto();
}
}
});
}
public void initialze()
{
imageView = (ImageView)findViewById(R.id.image);
button = (Button) findViewById(R.id.camera);
}
public void takePhoto()
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAPTURE_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
switch (requestCode)
{
case CAPTURE_PHOTO:
Toast.makeText(this, "Saved to Gallery", Toast.LENGTH_SHORT).show();
bitmap = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
break;
default:
break;
}
}
}
我在编写保存到图库代码时遇到问题。我在哪里调用保存到图库功能,android 中是否有一个内置功能可以帮助我做到这一点。
在您的 void initialze()
方法中调用 saveImagetoGallery
函数:
public void initialze()
{
imageView = (ImageView)findViewById(R.id.image);
button = (Button) findViewById(R.id.camera);
}
public void takePhoto()
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAPTURE_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
switch (requestCode)
{
case CAPTURE_PHOTO:
Toast.makeText(this, "Saved to Gallery", Toast.LENGTH_SHORT).show();
bitmap = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
saveImagetoGallery(bitmap); // Added this
break;
default:
break;
}
}
}
saveImagetoGallery
函数是:
private void saveImagetoGallery(Bitmap finalBitmap){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root+"/saveImage");
myDir.mkdirs();
Random random = new Random();
int n = 10000;
n = random.nextInt(n);
String imageName = "Image"+n+".jpg";
File file = new File(myDir,imageName);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
if(file.exists())file.delete();
try{
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG,90,out);
String string = file.getAbsolutePath();
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我假设您已经在 Android.Manifest.xml
文件中写入权限。
如果还没有,请添加:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我正在尝试用相机拍照并将其保存到图库中。 这是我到目前为止所做的:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Main5Activity.this,
new String[]{android.Manifest.permission.CAMERA}, RequestCameraPermissionID);
}
}
if (ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Main5Activity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERM_WRITE_STORAGE);
} else {
takePhoto();
}
}
});
}
public void initialze()
{
imageView = (ImageView)findViewById(R.id.image);
button = (Button) findViewById(R.id.camera);
}
public void takePhoto()
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAPTURE_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
switch (requestCode)
{
case CAPTURE_PHOTO:
Toast.makeText(this, "Saved to Gallery", Toast.LENGTH_SHORT).show();
bitmap = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
break;
default:
break;
}
}
}
我在编写保存到图库代码时遇到问题。我在哪里调用保存到图库功能,android 中是否有一个内置功能可以帮助我做到这一点。
在您的 void initialze()
方法中调用 saveImagetoGallery
函数:
public void initialze()
{
imageView = (ImageView)findViewById(R.id.image);
button = (Button) findViewById(R.id.camera);
}
public void takePhoto()
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,CAPTURE_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
switch (requestCode)
{
case CAPTURE_PHOTO:
Toast.makeText(this, "Saved to Gallery", Toast.LENGTH_SHORT).show();
bitmap = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
saveImagetoGallery(bitmap); // Added this
break;
default:
break;
}
}
}
saveImagetoGallery
函数是:
private void saveImagetoGallery(Bitmap finalBitmap){
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root+"/saveImage");
myDir.mkdirs();
Random random = new Random();
int n = 10000;
n = random.nextInt(n);
String imageName = "Image"+n+".jpg";
File file = new File(myDir,imageName);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
if(file.exists())file.delete();
try{
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG,90,out);
String string = file.getAbsolutePath();
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我假设您已经在 Android.Manifest.xml
文件中写入权限。
如果还没有,请添加:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />