1. 程式人生 > >相簿選取圖片插入到EditText中,實現圖文混排

相簿選取圖片插入到EditText中,實現圖文混排

1.首先用到了 ImagePicker,上github搜尋仿微信圖片選擇就能找到

2.匯入ImagePicker的包 compile 'com.squareup.picasso:picasso:2.5.2'

3.我是用是 picasso載入圖片,github上imagePicker的demo裡分別使用了三種載入圖片的第三方,glide,picasso,imageload,我使用的picasso
compile 'com.squareup.picasso:picasso:2.5.2'

4.在application初始化

ImagePicker imagePicker = ImagePicker.getInstance
(); imagePicker.setImageLoader(new PicassoImageLoader()); //設定圖片載入器 imagePicker.setShowCamera(true); //顯示拍照按鈕 imagePicker.setCrop(true); //允許裁剪(單選才有效) imagePicker.setSaveRectangle(true); //是否按矩形區域儲存 imagePicker.setSelectLimit(9); //選中數量限制 imagePicker.setStyle(CropImageView.Style.RECTANGLE); //裁剪框的形狀
imagePicker.setFocusWidth(800); //裁剪框的寬度。單位畫素(圓形自動取寬高最小值) imagePicker.setFocusHeight(800); //裁剪框的高度。單位畫素(圓形自動取寬高最小值) imagePicker.setOutPutX(1000);//儲存檔案的寬度。單位畫素 imagePicker.setOutPutY(1000);//儲存檔案的高度。單位畫素


 

 
建立PicassoImageLoader,下面會用到
繼承ImageLoader
@Override
public void displayImage(Activity activity, String path, ImageView imageView, int 
width, int height) { Picasso.with(activity) .load(Uri.fromFile(new File(path))) .placeholder(R.mipmap.default_image) .error(R.mipmap.default_image) .resize(width, height) .centerInside() .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) .into(imageView); } @Override public void clearMemoryCache() { //這裡是清除快取的方法,根據需要自己實現 } 6.在Activity中
editText = (EditText) findViewById(R.id.edit);
gridView = (GridView) findViewById(R.id.grid);
imagePicker = ImagePicker.getInstance();
imagePicker.setImageLoader(new PicassoImageLoader());

製造點選事件跳轉,

 
ImageGridActivity這個類是匯入的PmagePicker的包裡面封裝好的,能跳轉到相簿實現多張圖片選取

public void BtIntent(View v) {
    Intent intent = new Intent(this, ImageGridActivity.class);
    startActivityForResult(intent, 0);
}

//重寫onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
        if (data != null && requestCode == 0) {
            ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
            //可以用gridView展示選擇的圖片
            // MyAdapter adapter = new MyAdapter(images,MainActivity.this);
            //gridView.setAdapter(adapter);
           for (int i = 0; i < images.size(); i++) {
                //迴圈拿到路徑
               String path = images.get(i).path;
                insertEdit(path);
            }
        } else {
            Toast.makeText(this, "沒有資料", Toast.LENGTH_SHORT).show();
        }
    }
}


//實現圖片插入到EditeText中
public void insertEdit(String path) {
        Bitmap loadedImage = BitmapFactory.decodeFile(path);
        loadedImage = Bitmap.createBitmap(loadedImage, 0, 0, loadedImage.getWidth(), loadedImage.getHeight(), new Matrix(), true);
        ImageSpan imageSpan = new ImageSpan(MainActivity.this, loadedImage);
        SpannableString spannableString = new SpannableString(path);
        spannableString.setSpan(imageSpan, 0, path.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 將選擇的圖片追加到EditText中游標所在位置
        int index = editText.getSelectionStart();
// 獲取游標所在位置
        Editable edit_text = editText.getEditableText();
        if (index < 0 || index >= edit_text.length()) {
            edit_text.append(spannableString);
        } else {
            edit_text.insert(index, spannableString);
        }

    }

7.別忘了新增許可權
<uses-permission android:name="android.permission.CAMERA" />