[英]how to resize image from gallery
我想從圖庫中選擇圖像並將其發送到Second activity,但是圖像太大。
我需要調整大小,但我不知道該怎么做:
buttonIntent.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), REQUEST_GALLERY);
方法結果
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_GALLERY && resultCode == RESULT_OK) {
Uri uri = data.getData();
try {
bitmap = Media.getBitmap(this.getContentResolver(), uri);
imageView1.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
btnok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(Showpic_resumeActivity.this,Showdata_result_resume.class);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byteArray", bs.toByteArray());
startActivity(i);
}
});
第二項活動
if (getIntent().hasExtra("byteArray")) {
Bitmap b = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"), 0, getIntent()
.getByteArrayExtra("byteArray").length);
image_resume.setImageBitmap(b);
}
Passing bitmaps
從一種活動Passing bitmaps
到另一種活動是有risk
。 最好的方法是只pass Uri
而不是將位圖從第一個活動傳遞到第二個活動,然后在需要時將Uri轉換為位圖。
首先收集URI
Uri uri = data.getData();
並將URI從一個活動傳遞給另一個
btnok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(Showpic_resumeActivity.this,Showdata_result_resume.class);
i.setData(uri );// Passing the URI
startActivity(i);
}
});
* 並在Showdata_result_resume活動中獲取URI *
Uri uri=getIntent().getData();
YOUR_IMAGVIEW.setImageURI(uri);// Set URI to your ImageView
試試這個代碼:
Bitmap bmp=BitmapFactory.decodeResource(getResources(), your_image_loc);//ex:R.drawable.image1
int width=200;
int height=200;
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.