Android编程名称图片

时间:2014-03-24 08:48:55

标签: android camera

我想以编程方式为通过相机拍摄的图像指定自定义名称。我使用了how to control/save images took with android camera programmatically?给出的代码片段,但它仍然不起作用,图像以默认名称存储。为什么呢?

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Button cameraButton = (Button) findViewById(R.id.takepicture);

        cameraButton.setOnClickListener( new OnClickListener(){ 
            public void onClick(View v ){
                //Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                //startActivityForResult(intent,0);
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                                "pic_"+ String.valueOf(System.currentTimeMillis()) + ".jpg"));

                intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
                startActivityForResult(intent,0);
            }
        });
    }
}

3 个答案:

答案 0 :(得分:0)

你可以尝试如下......

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    String fullPath = "";
    File imageFile = null;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String pictureName = "IMG_" + timeStamp + ".jpg";

    fullPath = Environment.getExternalStorageDirectory() + "/ImageFolder/" + pictureName;

    File dir = new File(Environment.getExternalStorageDirectory() + "/ImageFolder/");
    dir.mkdirs();

    imageFile = new File(fullPath);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
    startActivityForResult(takePictureIntent, 1);

答案 1 :(得分:0)

试试这个

public void CapturePhoto() 
    {
        try
        {
            myFilesDir = new File( getFilesDir()+"/MyDir");
         if(! mfilesDir.exists() )
            myFilesDir.mkdirs();

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(myFilesDir.toString()+"/temp.jpeg")));

        startActivityForResult(intent, CAPTURE_IMAGE_CALLBACK);
    }
    catch ( ActivityNotFoundException e )
    {
        Log.e( TAG, "No camera: " + e );
    }
    catch ( Exception e )
    {
        Log.e( TAG, "Cannot make photo: " + e );
    }
}`

答案 2 :(得分:0)

试试这个:我在这里保存了当前日期和时间的图像名称:

public class CameraActivity extends Activity {

    private static final int CAMERA_REQUEST = 0;
    static Uri capturedImageUri=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_junit_test);
        Button cameraButton = (Button) findViewById(R.id.cameraButton);

        cameraButton.setOnClickListener( new OnClickListener(){ 
            private int CAMERA_RESULT;

            public void onClick(View v ){
                Calendar cal = Calendar.getInstance();
                File file = new File(Environment.getExternalStorageDirectory(),  (cal.getTimeInMillis()+".jpg"));
                if(!file.exists()){
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }else{
                    file.delete();
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                capturedImageUri = Uri.fromFile(file);
                Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                i.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
                startActivityForResult(i, CAMERA_RESULT);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST) {  
            //Bitmap photo = (Bitmap) data.getExtras().get("data");
            //imageView.setImageBitmap(photo);
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap( getApplicationContext().getContentResolver(),  capturedImageUri);
                //imageView.setImageBitmap(bitmap);
                Toast.makeText(this, "Image Saved As"+bitmap, Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } 
    }
}