我拍照后如何才能获得路径?

时间:2014-06-17 13:41:33

标签: android camera sd-card

我想开发拍摄照片的应用程序,然后将其存储在ImageView上,就像预览图像一样。 之后我想将uri路径保存到dataBase。我知道如何拍照以及如何将其保存在我的SD卡上但是如何获取特定图像以将其保存在我的数据库表上或者在imageView上实时创建预览? 在这里我的代码:

public class Add_item extends Activity 
                      implements SurfaceHolder.Callback , OnClickListener {

    EditText d_edName,d_edPrice,d_edInfo;
    ImageView d_image_pre1,d_image_pre2,d_image_pre3;
    Button d_BTakePicture, d_bSave;

    Camera camera;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;

    private PictureCallback rawCallback;
    private ShutterCallback shutterCallback;
    private PictureCallback jpegCallback;
    private String tag;

    private File myGeneralFolder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_additem);

        d_edName = (EditText) findViewById(R.id.d_name);

        d_edPrice = (EditText) findViewById(R.id.d_price);
        d_edInfo = (EditText)findViewById(R.id.d_info);
        d_image_pre1 = (ImageView) findViewById(R.id.d_image1);
        d_image_pre2 = (ImageView) findViewById(R.id.d_image2);
        d_image_pre3 = (ImageView) findViewById(R.id.d_image3);

        d_BTakePicture = (Button) findViewById(R.id.d_bTakePicture);
        d_BTakePicture.setOnClickListener(this);
        d_bSave = (Button) findViewById(R.id.d_bSave);
        d_bSave.setOnClickListener(this);

        surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();

        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        rawCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {
                Log.d("Log", "onPictureTaken - raw");
            }
        };

        shutterCallback = new ShutterCallback() {
            public void onShutter() {
                Log.i("Log", "onShutter'd");
            }
        };

        jpegCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {
                FileOutputStream outStream = null;

                try {
                    outStream = new FileOutputStream(String.format(myGeneralFolder+"/%d.jpg",(System.currentTimeMillis())));
                    outStream.write(data); 
                    outStream.close();

                    Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }
                Log.d("Log", "onPictureTaken - jpeg");
            }
        };    
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.d_bSave:
            String name = d_edName.getText().toString();
            String price = d_edPrice.getText().toString();
            String info = d_edInfo.getText().toString();

            Handler hand = new Handler(Add_item.this);
            hand.open();
            hand.addItem(new Item(0, name, info, "", price));
            hand.close();
            start_camera();
            break;

        case R.id.d_bTakePicture:
            if(d_edName.getText().toString().equals("")){
                Toast.makeText(Add_item.this, "No item name, folder cannot created!", 2000).show();
            }else{
            myGeneralFolder = new  File(Environment.getExternalStorageDirectory()+File.separator+"mySalesImages"+File.separator+d_edName.getText().toString());
            myGeneralFolder.mkdirs();
            captureImage(); 
            }
            break;

        default:
            break;
        }       
    }

    private void start_camera()
    {
            try{
                camera = Camera.open();
            }catch(RuntimeException e){
                Log.e(tag, "init_camera: " + e);
                return;
            }
            Camera.Parameters param;
            param = camera.getParameters();
            //modify parameter
            param.setPreviewFrameRate(20);
            param.setPreviewSize(176, 144);
            camera.setParameters(param);
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                //camera.takePicture(shutter, raw, jpeg)
            } catch (Exception e) {
                Log.e(tag, "init_camera: " + e);
                return;
            }
     }

    private void stop_camera()
    {
          camera.stopPreview();
          camera.release();
    }

    private void captureImage() {
          // TODO Auto-generated method stub
          camera.takePicture(shutterCallback, rawCallback, jpegCallback);       
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}

我的错误:

06-17 19:12:32.416: W/System.err(23714): java.net.MalformedURLException: Protocol not found: /storage/emulated/0/mySalesImages/hggg/1403021552392.jpg
06-17 19:12:32.426: W/System.err(23714):    at java.net.URL.<init>(URL.java:178)
06-17 19:12:32.426: W/System.err(23714):    at java.net.URL.<init>(URL.java:127)
06-17 19:12:32.426: W/System.err(23714):    at com.example.ordersapp.Add_item$3.onPictureTaken(Add_item.java:115)
06-17 19:12:32.426: W/System.err(23714):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:851)
06-17 19:12:32.426: W/System.err(23714):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 19:12:32.426: W/System.err(23714):    at android.os.Looper.loop(Looper.java:137)
06-17 19:12:32.426: W/System.err(23714):    at android.app.ActivityThread.main(ActivityThread.java:5419)
06-17 19:12:32.426: W/System.err(23714):    at java.lang.reflect.Method.invokeNative(Native Method)
06-17 19:12:32.426: W/System.err(23714):    at java.lang.reflect.Method.invoke(Method.java:525)
06-17 19:12:32.426: W/System.err(23714):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
06-17 19:12:32.426: W/System.err(23714):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
06-17 19:12:32.426: W/System.err(23714):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

不确定你究竟是什么意思,但这就是你如何设置文件路径:

FileOutputStream outStream = new FileOutputStream(path + filename,true);

所以在你的例子中:

outStream = new FileOutputStream(String.format(myGeneralFolder+"/%d.jpg",(System.currentTimeMillis())));

这个String.format(myGeneralFolder+"/%d.jpg"将是我猜的路径。所以将它赋予一个可变的并使用变量。

继续这样做:

    try{    
    String currentPicPath = String.format(myGeneralFolder+"/%d.jpg",(System.currentTimeMillis()));
        ImageView i = (ImageView)findViewById(R.id.image);
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(currentPicPath ).getContent());
        i.setImageBitmap(bitmap);
 } catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}
祝你好运

您的问题也在这里得到解答:Android, Make an image at a URL equal to ImageView's image

答案 1 :(得分:0)

你可以在Android文档中找到完整说明的示例http://developer.android.com/guide/topics/media/camera.html如何启动摄像头,如何监听结果以及结果中需要什么类型的数据