0

我想使用默认相机并捕获图像。我想将图像保存在 sdcard/photofolder/ 中,并将文件名保存为 sdcard/photofolder/ 中的当前时间格式,还在 imageview 页面中显示捕获图像

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);

            }  
        }

有什么想法吗?提前感谢

4

6 回答 6

2

您可以获得 SDCard 路径,例如..

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

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

然后您可以将文件保存到root + "/photofolder/20120830025700.jpg".


您是否请求了写入 SD 卡的权限?将以下字符串添加到您的应用清单:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-08-30T09:52:24.187 回答
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;
        }
    }
于 2012-08-30T09:22:35.050 回答
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; 
    } 
于 2012-08-30T09:23:08.603 回答
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");
     }  
}
于 2012-08-30T09:19:45.797 回答
0

作为此处给出的示例,请尝试此代码...

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);

路径将返回您的图像在 sdcard 中的位置

于 2012-08-30T09:20:29.023 回答
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);
 }
 }
 }
于 2012-08-30T09:21:12.833 回答