จะบันทึกรูปภาพจาก Camera ใน Android ไปยังโฟลเดอร์เฉพาะได้อย่างไร

โดยพื้นฐานแล้ว สิ่งที่ฉันต้องการทำคืออนุญาตให้ผู้ใช้สร้าง folder ของตัวเอง จากนั้นไปที่ activity ที่มี button เพื่อเปิดใช้ camera

จากนั้น ฉันต้องการที่จะเปิด camera และบันทึกรูปภาพ camera ลงในโฟลเดอร์ที่สร้างขึ้นใหม่

ฉันประสบปัญหาในส่วนสุดท้ายของการบันทึกรูปภาพ camera ลงในโฟลเดอร์ที่สร้างขึ้นใหม่

นี่คือ 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);

        }
    });

จากที่นี่ ฉันจะเปลี่ยนไปสู่กิจกรรมนี้:

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);
        }
    });         
    }
}

โปรดบอกวิธีบันทึกรูปภาพจาก camera ลงในโฟลเดอร์ที่สร้างขึ้นใหม่

ฉันต้องการให้ผู้ใช้สามารถถ่ายภาพ ภาพ ได้หลายภาพ จากนั้นจึงบันทึก ภาพ หลายภาพเหล่านี้ลงในโฟลเดอร์เฉพาะนั้น


person Adi Ten    schedule 01.09.2011    source แหล่งที่มา


คำตอบ (3)


เพิ่มรหัสนี้ก่อนเรียกกิจกรรมกล้อง

Uri uriSavedImage=Uri.fromFile(new File("/sdcard/flashCropped.png"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(camera, 1);
person ilango j    schedule 01.09.2011
comment
ผลลัพธ์คืออะไรและเหตุใดจึงใช้งานได้ เราไม่ควรใช้ค่าคงที่บางชนิดใช่ไหม - person Andrew S; 27.05.2013
comment
MediaStore.EXTRA_OUTPUT เป็นค่าคงที่ - person Teodor; 05.06.2013
comment
ไม่ทำงาน. มีบางอย่างผิดปกติกับ EXTRA_OUTPUT แน่นอน - person stdout; 30.10.2015
comment
แก้ไขคำตอบของคุณจาก ปัญหาที่สับสน บอกว่า camera เป็นตัวแปร intent ในรหัสของคุณ - person hossein masomzadeh; 21.04.2018

คุณควรเพิ่มตำแหน่งไฟล์ให้กับจุดประสงค์ในการจับภาพ ตัวอย่างเช่น:

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

ดูที่นี่

person Lior Ohana    schedule 01.09.2011

ลองสิ่งนี้....

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();
            }
        }
    }
person Richa    schedule 01.09.2011
comment
CAPTURE_IMAGE_ACTIVITY ไม่มีอะไรนอกจากจำนวนเต็ม คุณสามารถใช้ค่า interger อย่างง่ายที่คุณเลือกว่า 99/7/67 อะไรก็ได้ ใช้สำหรับตรวจสอบรหัสคำขอ ตัวอย่างเช่น 1 กิจกรรมสามารถเริ่มกิจกรรมอื่นๆ ได้มากมาย แต่ทุกคำขอควรมีรหัสคำขอที่ไม่ซ้ำกัน ซึ่งสามารถตรวจสอบได้เมื่อส่งคืนทุกกิจกรรมภายใน onActivityResult ในกิจกรรมหลักของคุณ ตรวจสอบรหัสคำขอเพื่อดูว่ากิจกรรมใดได้ส่งคืนแล้ว - person Kuntal Basu; 27.11.2013