2011-09-10 56 views
0

我做了一個演示,用cam拍攝照片,保存圖像,然後在其他活動中顯示最後一張照片製作成照片。這對模擬器來說是可以的,但是當我在真實手機中安裝我的演示程序時,我可以製作圖片,但保存的文件大小爲O KB。用tha照相機拍照,但圖像大小爲0 KB

//This is the method where I make the photo 
    private boolean makePhoto(){ 
     try{ 
      ImageCaptureCallback camDemo = null; 
      SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS"); 
      String filenameTimeStamp = timeStampFormat.format(new Date()); 
      ContentValues values = new ContentValues(); 
      values.put(MediaColumns.TITLE, String.format("%s.jpg", filenameTimeStamp)); 
      values.put(ImageColumns.DESCRIPTION, "Imagen desde Android Emulator"); 
      Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); 

      Log.d("titulo: ", values.get(MediaColumns.TITLE).toString()); 
      camDemo = new ImageCaptureCallback(getContentResolver().openOutputStream(uri)); 
      this.camera.takePicture(this.mShutterCallback, this.mPictureCallback, camDemo); 
      Log.d("makePhoto", "Foto hecha"); 
      return true; 
     }catch(Exception ex){ 
      ex.printStackTrace(); 
      Context context = getApplicationContext();   
      int duration = Toast.LENGTH_SHORT; 
      Toast toast = Toast.makeText(context, ex.toString(), duration); 
      toast.show(); 
     }  
     return false; 
    } 

    //This is the object where the pic taken is saved 
public class ImageCaptureCallback implements PictureCallback { 
    private OutputStream fileoutputStream; 

    public ImageCaptureCallback(OutputStream fileoutputStream){ 
     this.fileoutputStream = fileoutputStream; 
    } 

    public void onPictureTaken(byte[] data, Camera camera){ 
     try{ 
      BitmapFactory.Options options=new BitmapFactory.Options(); 
      options.inSampleSize = 1; 

      Bitmap myImage = BitmapFactory.decodeByteArray(data, 0, data.length,options); 


      BufferedOutputStream bos = new BufferedOutputStream(this.fileoutputStream); 

      myImage.compress(CompressFormat.JPEG, 75, bos); 

      bos.flush(); 
      bos.close(); 

     }catch (Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 

} 

發生了什麼事?

+0

任何錯誤日誌添加下面startActivityForResule? –

+0

要顯示的任何代碼? – EboMike

+0

你可以給我們的代碼?你在使用PictureCallback嗎? –

回答

0

我向您發送拍照代碼並將照片帶入您的應用程序。

首先添加如下打算:

File imageFile = new File(imageFilePath); 
Uri imageFileUri = Uri.fromFile(imageFile); 
Intent i = new Intent(
    android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
startActivityForResult(i, CAMERA_PIC_REQUEST); 

然後在你的代碼

protected void onActivityResult(int requestCode, int resultCode, 
      Intent imageReturnedIntent) { 
     super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
     switch (requestCode) 
{ 
case SELECT_PICTURE: 

Uri selectedImage = imageReturnedIntent.getData(); 
String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
Cursor cursor = getContentResolver().query(selectedImage, 
filePathColumn, null, null, null); 
cursor.moveToFirst(); 
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
// file path of selected image 
String filePath = cursor.getString(columnIndex); 
cursor.close(); 
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 
encode = Base64.encodeBytes(byteArray); 
try { 
    byte[] decode = Base64.decode(encode); 
    Bitmap bmp = BitmapFactory.decodeByteArray(decode, 0, 
      decode.length); 
    imgview_photo.setImageBitmap(bmp); 
    btn_getphoto.setVisibility(View.INVISIBLE); 
    btn_cancel.setVisibility(View.VISIBLE); 
    btn_upload.setVisibility(View.VISIBLE); 
    } 
catch (IOException e) { 
    e.printStackTrace(); 
} 
break; 
case CAMERA_PIC_REQUEST: 
    Bitmap bmp = BitmapFactory.decodeFile(imageFilePath); 
    int width = bmp.getWidth(); 
    int height = bmp.getHeight(); 
    float scaleWidth = ((float) 300)/width; 
    float scaleHeight = ((float) 300)/height; 
    Matrix matrix = new Matrix(); 
    matrix.postScale(scaleWidth, scaleHeight); 
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, 
     height, matrix, true); 
    ByteArrayOutputStream baostream = new ByteArrayOutputStream(); 
    resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, baostream); 
    byte[] byteArrays = baostream.toByteArray(); 
    encode = Base64.encodeBytes(byteArrays); 
    try { 
     byte[] decode = Base64.decode(encode); 
     Bitmap bitmp = BitmapFactory.decodeByteArray(decode, 0, 
       decode.length); 
     imgview_photo.setImageBitmap(bitmp); 
     btn_getphoto.setVisibility(View.INVISIBLE); 
     btn_cancel.setVisibility(View.VISIBLE); 
     btn_upload.setVisibility(View.VISIBLE); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
} 
+0

感謝您的代碼,但我可以製作圖片,我可以用模擬器顯示圖片,但是當我用手機拍攝圖片時,我保存了一個大小爲O KB的文件。我在代碼中做錯了什麼?在此先感謝JoséCarlos –