2011-01-19 90 views
3

我有一個應用程序可以通過底部的完整代碼拍攝照片。我的問題是關於如何訪問存儲在jpeg中的縮略圖或生成新的縮略圖。將代碼放在這個類中,或者是一個單獨的類,它根據事件或時間表檢查文件夾並生成縮略圖沒有問題。在Android中拍攝照片後保存縮略圖

具體來說,我已經設置了那裏有一個縮略圖,但無論如何無法訪問它。我嘗試過實施ExifInterface,並嘗試手動調整照片大小。我能夠將其他參數放入Jpeg Exif(將一些隨機數字放入經度和緯度)。查看文件確認數據已寫入。非常感謝。

public void surfaceChanged(SurfaceHolder holder,int format, int width, int height) { 
     Camera.Parameters parameters=camera.getParameters(); 

     parameters.setPreviewSize(width, height); 
     parameters.setPictureFormat(PixelFormat.JPEG); 
     parameters.setJpegThumbnailQuality(50); 
     parameters.setJpegThumbnailSize(192, 256); 

     camera.setParameters(parameters); 
     camera.startPreview(); 
    } 

這是我嘗試和使用將Exif接口失敗:

   ExifInterface myEI = new ExifInterface(photo.getPath()); 
       byte[] thumbArray = myEI.getThumbnail(); 

       File thumbFolder = new File(appFolder.getPath(), "thumbnails"); 
       if (!thumbFolder.exists()) 
       { 
        thumbFolder.mkdirs(); 
       } 
       File thumbnail=new File(thumbFolder, picFile.getName()); 
       if (thumbnail.exists()) { 
        thumbnail.delete(); 
       } 

       Bitmap bitmap = BitmapFactory.decodeByteArray(thumbArray, 0, thumbArray.length); 
       FileOutputStream fos = openFileOutput(thumbnail.getPath(), MODE_WORLD_READABLE); 
       bitmap.compress(CompressFormat.JPEG, 50, fos); 
       fos.close(); 

類的全部代碼。

public class PictureTaker extends Activity { 
private static final String TAG = "PictureTaker"; 
private SurfaceView preview=null; 
private SurfaceHolder previewHolder=null; 
private Camera camera=null; 
String filename; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera); 
    preview=(SurfaceView)findViewById(R.id.preview); 
    previewHolder=preview.getHolder(); 
    previewHolder.addCallback(surfaceCallback); 
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode==82 || keyCode==KeyEvent.KEYCODE_SEARCH) { 
     takePicture(); 
     return(true); 
    } 
    return super.onKeyDown(keyCode, event); 
} 

private void exitCamera() { 
    finish(); 
    super.onStop(); 
} 

private void takePicture() { 
    camera.takePicture(null, null, photoCallback); 
} 

SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() { 
    public void surfaceCreated(SurfaceHolder holder) { 
     camera=Camera.open(); 

     try { 
      camera.setPreviewDisplay(previewHolder); 
     } 
     catch (Throwable t) { 
      Log.d(TAG, "Exception in setPreviewDisplay()", t); 
     } 
    } 

    public void surfaceChanged(SurfaceHolder holder,int format, int width, int height) { 
     Camera.Parameters parameters=camera.getParameters(); 

     parameters.setPreviewSize(width, height); 
     parameters.setPictureFormat(PixelFormat.JPEG); 
     parameters.setJpegThumbnailQuality(50); 
     parameters.setJpegThumbnailSize(192, 256); 

     camera.setParameters(parameters); 
     camera.startPreview(); 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     camera.stopPreview(); 
     camera.release(); 
     camera=null; 
    } 
}; 

Camera.PictureCallback photoCallback=new Camera.PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) { 
     new SavePhotoTask().execute(data); 
     camera.startPreview(); 
    } 
}; 

class SavePhotoTask extends AsyncTask<byte[], String, String> { 
    @Override 
    protected String doInBackground(byte[]... jpeg) { 
     filename = Utilities.getTimeString() + ".jpg"; 

     File photo=new File(filename); 
     if (photo.exists()) { 
      photo.delete(); 
     } 
     try { 
      FileOutputStream fos=new FileOutputStream(photo.getPath()); 
      fos.write(jpeg[0]); 
      fos.close(); 

     } 
     catch (java.io.IOException e) { 
      Log.d(TAG, "Exception in photoCallback", e); 
     } 
     return(null); 
    } 
} 

}

回答

1

我知道這聽起來很愚蠢,但唯一我做不同於你正在初始化一個文件對象,而不是文件名的字符串的FileOutputStream中。那麼,你有

FileOutputStream fos = openFileOutput(thumbnail.getPath(), MODE_WORLD_READABLE); 
FileOutputStream fos=new FileOutputStream(photo.getPath()); 

試試這個:

FileOutputStream fos = openFileOutput(thumbnail, MODE_WORLD_READABLE); 
FileOutputStream fos=new FileOutputStream(photo); 

我好像記得,這使事情爲我工作的地方以前是不是,雖然我不能想象爲什麼它會令任何差異。

0

而不是

FileOutputStream fos = openFileOutput(thumbnail.getPath(), MODE_WORLD_READABLE); 
FileOutputStream fos=new FileOutputStream(photo.getPath()); 

試試這個

FileOutputStream fos = openFileOutput(thumbnail.getPath(), MODE_WORLD_READABLE); 
FileOutputStream fos=new FileOutputStream(new File(photo.getPath())); 
相關問題