中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

從相冊(cè)中選擇圖片進(jìn)行壓縮并用ImageView展示出來(lái)

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

  從手機(jī)中選擇照片這是幾乎所有應(yīng)用的功能之一,主要考慮到一點(diǎn)的就是如果圖片太大了,可能會(huì)OOM,簡(jiǎn)單的處理就是對(duì)圖片進(jìn)行壓縮!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/photo_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="選擇照片" />

    <ImageView
        android:id="@+id/photo_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:contentDescription="選擇自己相冊(cè)照片"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"/>

</LinearLayout>

-----------操作代碼--------------

public class PhotoActivity extends Activity {
/**點(diǎn)擊選擇照片按鈕*/
private Button photoBtn;
/**選擇后展示照片*/
private ImageView photoIv;
/**請(qǐng)求碼*/
private final static int SELECT_PHOTO_CODE = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
initViews();
initListners();
}


private void initViews() {
photoBtn = (Button) findViewById(R.id.photo_btn);
photoIv = (ImageView) findViewById(R.id.photo_iv);
}

private void initListners() {
photoBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
selectPhotoFromAblum();
}
});
}

/**從相冊(cè)庫(kù)中選擇圖片操作*/
private void selectPhotoFromAblum() {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, SELECT_PHOTO_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case SELECT_PHOTO_CODE:
if (null != data) {
/**取得圖片,圖片壓縮比為4*4*/
Bitmap bm = compressBitmap(null, null, this, data.getData(), 4, false);
photoIv.setImageBitmap(bm);
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

/**圖片壓縮處理,size參數(shù)為壓縮比,比如size為2,則壓縮為1/4**/
private Bitmap compressBitmap(String path, byte[] data, Context context, Uri uri, int size, boolean width) {
Options options = null;
if (size > 0) {
Options info = new Options();
/**如果設(shè)置true的時(shí)候,decode時(shí)候Bitmap返回的為數(shù)據(jù)將空*/
info.inJustDecodeBounds = false;
decodeBitmap(path, data, context, uri, info);
int dim = info.outWidth;
if (!width) dim = Math.max(dim, info.outHeight);
options = new Options();
/**把圖片寬高讀取放在Options里*/
options.inSampleSize = size;
}
Bitmap bm = null;
try {
bm = decodeBitmap(path, data, context, uri, options);
}
catch (Exception e) {
e.printStackTrace();
}
return bm;
}


/**把byte數(shù)據(jù)解析成圖片*/
private Bitmap decodeBitmap(String path, byte[] data, Context context, Uri uri, BitmapFactory.Options options) {
Bitmap result = null;
if (path != null) {
result = BitmapFactory.decodeFile(path, options);
}
else if (data != null) {
result = BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
else if (uri != null) {
ContentResolver cr = context.getContentResolver();
InputStream inputStream = null;
try {
inputStream = cr.openInputStream(uri);
result = BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}

最后注意權(quán)限:

    <!--  從SDCard讀取數(shù)據(jù)權(quán)限 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

標(biāo)簽: 代碼 權(quán)限

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:php直接在網(wǎng)頁(yè)上生成excel提供下載的方法

下一篇:一個(gè)簡(jiǎn)單的php分頁(yè)類代碼