无法在我的Android应用中保存照片

时间:2016-04-26 22:15:49

标签: android image file save

这是我第一次在stackoverflow中提问,并使用相机应用程序制作一个Android应用程序。

我试图在android开发页面中使用源代码,但它失败了。 感谢您阅读我的帖子。

    package com.examplehoon.gnibnumb;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import java.io.File;

    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

        static final int REQUEST_IMAGE_CAPTURE = 1;
        String mCurrentPhotoPath;
        static final int REQUEST_TAKE_PHOTO = 1;
        private File createImageFile() throws IOException {
            // Create an image file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
            File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
            );

            // Save a file: path for use with ACTION_VIEW intents
            mCurrentPhotoPath = "file:" + image.getAbsolutePath();
            return image;
        }


        private void dispatchTakePictureIntent() {

        // Create the File where the photo should go
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
            // Error occurred while creating the File

                }
        // Continue only if the File was successfully created

                Intent takePictureIntent = new                                         Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            Uri outputFileUri = Uri.fromFile(photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);




                }

                public void startClicked(View view) throws IOException,                 InterruptedException{
                    for(int i = 0; i<10; i++){
                        thd.run();
                        thd.sleep(5000);


                    }

                }

                public class Top implements Runnable{
                    public void run() {

                dispatchTakePictureIntent();



            }
        }

        Top t = new Top();
        Thread thd = new Thread(t);

    }

0 个答案:

没有答案