2012-04-02 33 views
12

thể trùng lặp:
Android Camera - Save image into a new folder in SD CardChụp hình với mục đích máy ảnh và lưu vào tập tin

tôi đang cố gắng để chụp hình và lưu nó vào một tập tin. Các vấn đề cames tôi đang cố gắng để lưu bitmap vào một tập tin. Đây là mã của tôi:

private void takePic() { 
    Intent cameraIntent = new Intent(
      android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, 2); 


} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (requestCode == 2) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      ImageView test = (ImageView) findViewById(R.id.test); 
      test.setImageBitmap(photo); 

      try { 
       FileOutputStream out = new FileOutputStream("filename"); 
       photo.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

Và trường hợp ngoại lệ trong logcat:

04-02 14:46:51.975: W/IInputConnectionWrapper(2225): showStatusIcon on inactive InputConnection 
04-02 14:46:56.135: W/System.err(2225): java.io.FileNotFoundException: /filename (Read-only file system) 
04-02 14:46:56.135: W/System.err(2225):  at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method) 
04-02 14:46:56.145: W/System.err(2225):  at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232) 
04-02 14:46:56.145: W/System.err(2225):  at java.io.FileOutputStream.<init>(FileOutputStream.java:94) 
04-02 14:46:56.145: W/System.err(2225):  at java.io.FileOutputStream.<init>(FileOutputStream.java:165) 
04-02 14:46:56.145: W/System.err(2225):  at java.io.FileOutputStream.<init>(FileOutputStream.java:144) 

Trả lời

4

lỗi của bạn nói rõ rằng java.io.FileNotFoundException: /filename

vui lòng cung cấp đường dẫn chính xác "/ sdcard/filename"

new FileOutputStream(getExternalStorageDirectory()+"filename"); 

HOẶC

String imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/name.png"; 

lưu ý: thêm quyền WRITE_EXTERNAL_STORAGE trong tệp kê khai.

+0

nó bệ phóng với tôi không có getExternalStorageDirectory() phương pháp – Darko

+0

xem, tôi đã cập nhật mã. –

+0

có nghĩa là bạn không có thẻ SD –

7

ry mã dưới đây là một trong những giải pháp cho vấn đề của bạn ::

static Uri capturedImageUri = null; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.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) { 
      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); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
+7

Luôn định dạng mã của bạn (CTRL + SHIFT + F vì chúa) khi đăng lên stackoverflow, tôi cần hai dòng mã này và tôi không tìm thấy chúng vì định dạng mã oor –

+0

làm cách nào tôi có thể nén nó trước khi lưu? –

Các vấn đề liên quan