如何将Android相机中的图像保存到特定文件夹?

发布于 2024-12-02 13:23:36 字数 2439 浏览 1 评论 0原文

基本上,我想要做的是允许用户创建自己的文件夹,然后转到包含按钮activity来启动相机

从那里我希望能够启动相机并将相机图像保存到新创建的文件夹中。

我在将相机图像保存到新创建的文件夹的最后部分时遇到问题。

这是我的代码

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {


        EditText text = (EditText)findViewById(R.id.editText1); 
        EditText text2 = (EditText)findViewById(R.id.editText2);



        @Override
        public void onClick(View v) {

            final String name = text.getText().toString();
            final String placeName = text2.getText().toString(); 

            String place = placeName.substring(0,3);
            String direct = name + place ;

            File folder = new File("/sdcard/CameraTest/" + direct + "/");
            folder.mkdirs();

            Intent myIntent = new Intent(CameraTestActivity.this, Press.class);
            myIntent.putExtra("key", "/sdcard/CameraTest/" + direct + "/");
            startActivity(myIntent);

        }
    });

从这里我过渡到此活动:

public class Press extends Activity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.afterenter);
        final String direct = this.getIntent().getStringExtra("key");


        // TODO Auto-generated method stub
        Button p = (Button) findViewById(R.id.button2);
        p.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent camera= new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(camera, 1);

            }
        });



    Button np = (Button) findViewById(R.id.button3);
    np.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent next = new Intent(Press.this, CameraTestActivity.class);
            startActivity(next);
        }
    });         
    }
}

请告诉我如何将相机中的图像保存到新创建的文件夹中。

我希望用户能够拍摄多张照片,然后将这些照片保存到该特定文件夹中。

Basically, what I want to do is allow the user to make their own folder and then go to an activity that contains a button to launch the camera.

From there I want to be able to launch the camera and save the camera images into the newly created folder.

I am having trouble with last part of saving the camera images into the newly created folder.

Here is my Code :

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {


        EditText text = (EditText)findViewById(R.id.editText1); 
        EditText text2 = (EditText)findViewById(R.id.editText2);



        @Override
        public void onClick(View v) {

            final String name = text.getText().toString();
            final String placeName = text2.getText().toString(); 

            String place = placeName.substring(0,3);
            String direct = name + place ;

            File folder = new File("/sdcard/CameraTest/" + direct + "/");
            folder.mkdirs();

            Intent myIntent = new Intent(CameraTestActivity.this, Press.class);
            myIntent.putExtra("key", "/sdcard/CameraTest/" + direct + "/");
            startActivity(myIntent);

        }
    });

From here I transition into this activity:

public class Press extends Activity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.afterenter);
        final String direct = this.getIntent().getStringExtra("key");


        // TODO Auto-generated method stub
        Button p = (Button) findViewById(R.id.button2);
        p.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent camera= new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(camera, 1);

            }
        });



    Button np = (Button) findViewById(R.id.button3);
    np.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent next = new Intent(Press.this, CameraTestActivity.class);
            startActivity(next);
        }
    });         
    }
}

Please tell me how to save the images from the camera into the newly created folder.

I want the user to be able to take several pictures and then save these several pictures into that specific folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小霸王臭丫头 2024-12-09 13:23:37

在调用相机活动之前添加此代码,

Uri uriSavedImage=Uri.fromFile(new File("/sdcard/flashCropped.png"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(camera, 1);

add this code before calling camera activity,

Uri uriSavedImage=Uri.fromFile(new File("/sdcard/flashCropped.png"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(camera, 1);
宁愿没拥抱 2024-12-09 13:23:37

试试这个......

path = Environment.getExternalStorageDirectory() + "/photo1.jpg";
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY);

并且您还没有实现 onActivityResult() 尝试一下这可能会对您有所帮助。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    System.gc();
    if (requestCode == CAPTURE_IMAGE_ACTIVITY) {
        if (resultCode == Activity.RESULT_OK) {
            try {
                // Call function MakeFolder to create folder structure if
                // its not created
                if(imageBitmap != null) {
                    imageBitmap = null;
                    imageBitmap.recycle();
                }
                MakeFolder();
                // Get file from temp1 file where image has been stored
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 3;
                imageBitmap = BitmapFactory.decodeFile(path, options);
                imgForPhotograph.setImageBitmap(imageBitmap);
                isImageTaken = true;
                // Name for image
                IMAGEPATH = getString(R.string.chassisImage)
                        + System.currentTimeMillis();
                SaveImageFile(imageBitmap,IMAGEPATH);
            } catch (Exception e) {
                Toast.makeText(this, "Picture Not taken",
                                Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    }

Try out this....

path = Environment.getExternalStorageDirectory() + "/photo1.jpg";
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY);

and you haven't implemented onActivityResult() Try out this may help you.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    System.gc();
    if (requestCode == CAPTURE_IMAGE_ACTIVITY) {
        if (resultCode == Activity.RESULT_OK) {
            try {
                // Call function MakeFolder to create folder structure if
                // its not created
                if(imageBitmap != null) {
                    imageBitmap = null;
                    imageBitmap.recycle();
                }
                MakeFolder();
                // Get file from temp1 file where image has been stored
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 3;
                imageBitmap = BitmapFactory.decodeFile(path, options);
                imgForPhotograph.setImageBitmap(imageBitmap);
                isImageTaken = true;
                // Name for image
                IMAGEPATH = getString(R.string.chassisImage)
                        + System.currentTimeMillis();
                SaveImageFile(imageBitmap,IMAGEPATH);
            } catch (Exception e) {
                Toast.makeText(this, "Picture Not taken",
                                Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    }
梦里°也失望 2024-12-09 13:23:37

您应该将文件位置添加到图像捕获意图中。
例如:

camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, [file location]);

查看此处

You should add the file location to the image capture intent.
For example:

camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, [file location]);

Take a look here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文