下面是我的代码,如果在设置为二次采样imageview之后从相机拍摄照片,则将图像尺寸设置为较小,而不是全屏。
public class MainActivity extends Activity {
Bitmap thumbnail;
private int REQUEST_CAMERA = 0;
private Button btnSelect;
private String userChoosenTask;
ArrayList<Uri> mArrayUri;
SubsamplingScaleImageView ivImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSelect = (Button) findViewById(R.id.btnSelectPhoto);
ivImage = (SubsamplingScaleImageView) findViewById(R.id.ivImage);
btnSelect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
mArrayUri = new ArrayList<Uri>();
}
private void selectImage() {
final CharSequence[] items = {"Take Photo",
"Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
cameraIntent();
}else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void cameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
thumbnail = (Bitmap) data.getExtras().get("data");
mArrayUri.add(getImageUri(MainActivity.this, thumbnail));
ivImage.setImage(ImageSource.bitmap(thumbnail).dimensions(4128, 3096));
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
}
Xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="0.1"
android:gravity="center">
<Button
android:id="@+id/btnSelectPhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Photo" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="2"
android:gravity="center"
android:orientation="vertical">
<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="@+id/ivImage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
Gradle添加这些行
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'
任何人都可以提前帮助我。
答案 0 :(得分:1)
private void onCaptureImageResult(Intent data) {
//mArrayUri.add(getImageUri(MainActivity.this, thumbnail));
String ImagePath = getRealPathFromURI(context, data.getData());
ivImage.setImageBitmap(BitmapFactory.decodeFile(ImagePath));
}
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();
}
}
}
答案 1 :(得分:0)
设置图片后,请致电ivImage.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP)
,您可以更改以下代码:
private void onCaptureImageResult(Intent data) {
thumbnail = (Bitmap) data.getExtras().get("data");
mArrayUri.add(getImageUri(MainActivity.this, thumbnail));
ivImage.setImage(ImageSource.bitmap(thumbnail).dimensions(4128, 3096));
ivImage.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
}
来自此imageView
库的configuration doc。
答案 2 :(得分:0)
基本上thumbnail = (Bitmap) data.getExtras().get("data");
将只返回拇指大小的图像,这对于仅在小拇指尺寸图像中设置很有用。如果您尝试在全屏幕中设置拇指大小图像,则可能会失真。因此,我建议您更改全尺寸图片的代码。
要获得全尺寸图像,请访问谷歌提供的相机意图演示应用程序链接。
https://developer.android.com/training/camera/photobasics.html