捕捉图像的相机在Android和保存自定义名称 [英] Capture image with camera in android and save with a custom name

查看:107
本文介绍了捕捉图像的相机在Android和保存自定义名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已创建Android应用程序,将捕捉画面并保存在SD卡的文件夹,现在我要保存的图像与自定义名称。

 进口java.io.ByteArrayOutputStream中;
进口android.view.Menu;
进口android.app.Activity;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.os.Bundle;
进口android.provider.MediaStore;
进口android.view.View;
进口android.widget.Button;
进口android.widget.ImageView;

公共类MainActivity延伸活动{

    私有静态最终诠释CAMERA_REQUEST = 1888;
    私人ImageView的ImageView的;

    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        this.imageView =(ImageView的)this.findViewById(R.id.imageView1);
        按钮photoButton =(按钮)this.findViewById(R.id.button1);

        photoButton.setOnClickListener(新View.OnClickListener()
        {

            @覆盖
            公共无效的onClick(视图v)
            {

                意图cameraIntent =新的意图(
                        android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,新光name.jpg);
                startActivityForResult(cameraIntent,CAMERA_REQUEST);
            }
        });
    }

    保护无效onActivityResult(INT申请code,INT结果code,意图数据)
    {
        如果(要求code == CAMERA_REQUEST和放大器;&安培;结果code == RESULT_OK)
        {
            。位图照片=(位图)data.getExtras()获得(数据);
            imageView.setImageBitmap(照片);
            MediaStore.Images.Media.insertImage(getContentResolver(),照片,
                    NULL,NULL);

            ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
            photo.com preSS(Bitmap.Com pressFormat.JPEG,100,BAOS);
            byte []的B = baos.toByteArray();

        }
    }
 

这里是我所使用的拍摄图像,并将它们保存在SD卡文件夹中的code,请帮我保存图像与一个特定的名称,例如:android.jpeg

解决方案

 文件不过outFile =新的文件(Environment.getExternalStorageDirectory(),myname.jpeg);
FileOutputStream中FOS =新的FileOutputStream(不过outFile);
photo.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
fos.flush();
fos.close();
 

您还需要添加权限的Andr​​oid清单。

 <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

该片段将节省的照片 / SD卡与名称的内容 myname.jpeg

i have create an android application that will capture picture and save in sdcard folder,now i want to save the image with a custom name.

import java.io.ByteArrayOutputStream;
import android.view.Menu;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private static final int CAMERA_REQUEST = 1888;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.imageView = (ImageView) this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);

        photoButton.setOnClickListener(new View.OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {

                Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, "new-photo-name.jpg");
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) 
        {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(photo);
            MediaStore.Images.Media.insertImage(getContentResolver(), photo,
                    null, null);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
            byte[] b = baos.toByteArray();

        }
    }

here is the code which i have used for capturing the image and save them in the sd card folder,please help me to save the image with a specific name for eg:android.jpeg

解决方案

File outFile = new File(Environment.getExternalStorageDirectory(), "myname.jpeg");
FileOutputStream fos = new FileOutputStream(outFile);
photo.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
fos.flush();
fos.close();

you would also need to add Permission in Android Manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

the snippet will save the content of photo inside /sdcard with the name "myname.jpeg"

这篇关于捕捉图像的相机在Android和保存自定义名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆