2016-08-20 75 views
0

使用下面的代碼,我要添加什麼來保存捕獲的圖像,並提取該圖像的URI以供立即使用;該用途明確地將URI添加到HashMap中,並且可能之後立即將其調用到imageview。如何捕捉圖像並保存(Uri)?

這裏是我的代碼:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, 0); 

onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == REQUEST_HAPPY_PHOTO || requestCode == REQUEST_SURPRISED_PHOTO || requestCode == REQUEST_AFRAID_PHOTO || 
       requestCode == REQUEST_UPSET_PHOTO || requestCode == REQUEST_SAD_PHOTO) { 
     final boolean isCamera; 
     if (data == null) { 
      isCamera = true; 
     } else { 
      final String action = data.getAction(); 
      if (action == null) { 
       isCamera = false; 
      } else { 
       isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      } 
     } 
     Uri selectedImageUri; 
     if (isCamera) { 
      selectedImageUri = outputFileUri; 
     } else { 
      selectedImageUri = data == null ? null : data.getData(); 
     } 

我也有對圖像的創建文件的方法:

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp; 
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
    File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
    return image; 
} 

回答

0

捕獲圖像for more

String mCurrentPhotoPath; 

private File createImageFile() throws IOException { 
// Create an image file name 
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
String imageFileName = "JPEG_" + timeStamp + "_"; 
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
File image = File.createTempFile(
    imageFileName, /* prefix */ 
    ".jpg",   /* suffix */ 
    storageDir  /* directory */ 
); 

// Save a file: path for use with ACTION_VIEW intents 
mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
return image; 
} 

您可以簡單地這樣做重命名圖像

File outFile = new File(Environment.getExternalStorageDirectory(), "imagename.jpeg"); 
FileOutputStream fos = new FileOutputStream(outFile); 
photo.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
fos.flush(); 
fos.close(); 

通過在Android的清單

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

添加權限檢索URI

Uri uri = Uri.fromFile(getFileStreamPath(outFile)); 

希望這將有助於

0

您在啓動意圖時傳遞文件,然後在捕獲後使用它。

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

全部演示工作的所有版本。

對於捕獲和挑選圖像

注:點擊安卓6.0 GET運行權限,然後procced。

public class MainActivity extends Activity { 

    private ImageView camera, gallery; 
    File photoFile; 
    Uri uri; 
    private final static int RESULT_LOAD_IMAGE = 1; 
    private final static int REQUEST_IMAGE_CAPTURE = 2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     camera = (ImageView) findViewById(R.id.Img); 
     gallery = (ImageView) findViewById(R.id.imageView); 
     camera.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dispatchTakePictureIntent(); 

      } 
     }); 
     gallery.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(i, RESULT_LOAD_IMAGE); 
      } 
     }); 
    } 

    private void dispatchTakePictureIntent() { 

     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      photoFile = Environment.getExternalStoragePublicDirectory("/myimage/save.jpg"); 
      if (photoFile != null) { 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
         Uri.fromFile(photoFile)); 
       startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
      } 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
      uri = data.getData(); 
      String selecteadImage = getRealPathFromURI(this, data.getData()); 
      Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show(); 

     } else if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 

      if (data != null && data.getData() != null) { 
       String selecteadImage = getRealPathFromURI(this, data.getData()); 
       Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show(); 
      } else { 
       if (photoFile != null) { 
        String selecteadImage = photoFile.getAbsolutePath(); 
        Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
    } 

    public String getRealPathFromURI(Context context, Uri contentUri) { 
     Cursor cursor = null; 
     try { 
      String[] proj = {MediaStore.Images.Media.DATA}; 
      cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } finally { 
      if (cursor != null) { 
       cursor.close(); 
      } 
     } 
    } 
} 

清單權限:

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

activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/image_1"> 


    <ImageView 
     android:id="@+id/camera" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:src="@mipmap/ic_launcher" /> 

    <ImageView 
     android:id="@+id/gallery" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/camera" 
     android:layout_centerHorizontal="true" 
     android:src="@mipmap/ic_launcher" /> 
</RelativeLayout> 
相關問題