2012-08-30 174 views
0

我想使用默認相機和捕捉圖像。我想保存圖像中的SD卡/ photofolder /和保存文件名,如SD卡/ photofolder /也顯示在imageview的頁面捕獲圖像如何在SD卡中保存捕獲圖像?

Uri outputFileUri = Uri.fromFile("/sdcard/Photofolder/");  
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 
startActivityForResult(intent, CAMERA_REQUEST); 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (requestCode == CAMERA_REQUEST) { 
       Bitmap photo = (Bitmap) data.getExtras().get("data"); 
       // imageView.setImageBitmap(photo); 

       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 4; 
       Bitmap bitmap = BitmapFactory.decodeFile(picFileName, options); 
       imageView.setImageBitmap(bitmap); 
       imageView.setVisibility(View.VISIBLE); 

      } 
     } 

任何想法,這個curenttime格式?感謝adavance

+0

** outputFileUri **的值是多少? – user370305

+0

從這個問題找到答案http://stackoverflow.com/questions/12179045/how-to-access-an-image-that-is-stored-internally/12179239#12179239 .. – user370305

+0

聰明...看看我的以上評論..你在OP的問題的答案... – user370305

回答

2

你可以得到的SD卡路徑喜歡..

String root = Environment.getExternalStorageDirectory().toString(); 

new File(root + "/photofolder").mkdirs(); 

然後你可以將文件保存到root + "/photofolder/20120830025700.jpg"


您是否要求有權寫入SD卡?將以下字符串添加到應用清單中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
0

下面的代碼可以幫助ü

private void takePicture() 
     { cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, IMAGE_CAPTURE); 
     } 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_REQUEST) { 
    File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 
    File output = new File(dir, "camerascript.png"); 
    } 
} 
+0

感謝兄弟,但我想保存圖像在SD卡/ photofolder /和保存圖像作爲當前時間格式,如示例SD卡/ photofolder/20120830025700.jpg – ckpatel

0

作爲例子給出here試試這個代碼...

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 
File output = new File(dir,"camera.png"); 

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output)); 
String path =output.getAbsolutePath(); 
startActivityForResult(cameraIntent, TAKE_PHOTO); 

路徑將在SD卡

0

使用回圖像的位置此代碼將圖像保存到SD卡中並創建目錄以存儲捕獲的圖像

File photo = new File(Environment.getExternalStorageDirectory()+"/photofolder"); 
    System.out.println(photo.toString()); 
    boolean success = false; 
    if(!photo.exists()) 
    { 
    success = photo.mkdirs(); 
    } 
    if(!success) 
     { 

     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     photo = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android/data/"+getApplicationContext().getPackageName()+"/files/Receipt", imagename+".png"); 
     Toast.makeText(this, photo.toString(), Toast.LENGTH_LONG); 
     intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo)); 
     Uri imageurl = Uri.fromFile(photo); 
     startActivityForResult(intent, CAMERA_RECEIPTREQUEST); 
     } 

要顯示拍攝的圖像使用下面的代碼

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    switch(requestCode) 
    { 
    case CAMERA_RECEIPTREQUEST: 
     if(resultCode== Activity.RESULT_OK) 
     { 
      if(data!=null) 
       { 
         BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inSampleSize = 8; 
       ImageView jpgView = (ImageView)findViewById(R.id.imageView1); 
       Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options); 
        jpgView.setImage(receipt); 
} 
} 
} 
0

試試這個

private void cameraOn() { 
      tempUri = "sdcard/......"; 
     i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     i.putExtra(MediaStore.EXTRA_OUTPUT, tempUri); 
     startActivityForResult(i, cameraData); 
    } 

爲了得到位圖

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     switch (requestCode) { 
     case cameraData: 
      if (resultCode == RESULT_OK) { 
       Bitmap bmp = BitmapFactory.decodeFile(tempUri.getPath(), null); 
       imageShow.setImageBitmap(bmp); 
      } 
      break; 
     } 
    } 
0

嘗試在onActivityResult使用這個()

setupImage(photo); 

public Bitmap setupImage(Intent data) { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = SAMPLE_SIZE;  // SAMPLE_SIZE = 2 

     Bitmap tempBitmap = null; 
     Bitmap bm = null; 
     try { 
     tempBitmap = (Bitmap) data.getExtras().get("data"); 
     bm = tempBitmap; 

     Log.v("ManageImage-hero", "the data.getData seems to be valid"); 

     FileOutputStream out = new FileOutputStream(outputFileUri.getPath()); 
     tempBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
     } catch (NullPointerException ex) { 
     Log.v("ManageImage-other", "another phone type"); 
     bm = otherImageProcessing(options); 
     } catch(Exception e) { 
     Log.v("ManageImage-setupImage", "problem setting up the image"+e); 
     } 

     return bm; 
    } 
private Bitmap otherImageProcessing(BitmapFactory.Options options) { 
     Bitmap bm = null; 

     try { 
     FileInputStream fis = new FileInputStream(outputFileUri.getPath()); 
     BufferedInputStream bis = new BufferedInputStream(fis); 
     bm = BitmapFactory.decodeStream(bis, null, options); 

     // cleaning up 
     fis.close(); 
     bis.close(); 
     } catch(Exception e) { 
     Log.e("ManageImage-otherImageProcessing", "Cannot load image", e); 
     } 

     return bm; 
    }