我正在尝试从图库中获取图片。当用户从图库中选择图片时,他们需要询问裁剪图像,然后裁剪的图像应该显示在ImageView中。
这是我试过的:
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Details extends AppCompatActivity {
ImageView i1,i2;
int num =2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
android.support.v7.app.ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.hide();
}
i1 = (ImageView)findViewById(R.id.prof1);
i2 = (ImageView)findViewById(R.id.prof2);
i1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent img_uopload = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
img_uopload.putExtra("crop","true");
img_uopload.putExtra("aspectX",1);
img_uopload.putExtra("aspectY",1);
img_uopload.putExtra("outputX",200);
img_uopload.putExtra("outputY",200);
img_uopload.putExtra("return-data",true);
startActivityForResult(img_uopload,num);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == num && resultCode == RESULT_OK ){
if(data!=null){
Bundle extras = data.getExtras();
if(extras !=null){
Bitmap img = extras.getParcelable("data");
i1.setImageBitmap(img);
}
else{
Toast.makeText(getApplicationContext(),"Not Read",Toast.LENGTH_LONG).show();
}
}
}
}
}
此处bundle值返回null。
在android清单中,我已经包含了。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
UAC:
答案 0 :(得分:0)
您可以使用裁剪意图检查:
try {
String picUri="your irl";
Intent myCropIntent = new Intent("com.android.camera.action.CROP");
myCropIntent.setDataAndType(picUri, "image/*");
myCropIntent.putExtra("crop", "true");
myCropIntent.putExtra("aspectX", 1);
myCropIntent.putExtra("aspectY", 1);
myCropIntent.putExtra("outputX", 128);
myCropIntent.putExtra("outputY", 128);
myCropIntent.putExtra("return-data", true);
startActivityForResult(myCropIntent, CROP_PIC_REQUEST_CODE);
}
catch (ActivityNotFoundException e) {
String errorMessage = "No Activity found";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
否则您也可以使用此裁剪库simple-crop-image-lib
圆角图片视图使用此库RoundedImageView