多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > 綜合技術(shù) > Android相機(jī)相冊(cè)的調(diào)用,圖片的存儲(chǔ)刪除

Android相機(jī)相冊(cè)的調(diào)用,圖片的存儲(chǔ)刪除

來源:程序員人生   發(fā)布時(shí)間:2016-11-17 09:30:10 閱讀次數(shù):2563次

主要以SimpleFilter的源碼為例子詳解,app可在百度利用商店下載
SimpleFilter源代碼下載

xml

<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/iv_change" android:layout_width="fill_parent" android:scaleType="fitXY" android:layout_height="220dp" android:maxWidth="1000dp" android:maxHeight="1000dp" android:src="@drawable/origin" /> <SeekBar android:id="@+id/sb_alpha" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_margin="10dp" /> <SeekBar android:id="@+id/sb_red" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_margin="10dp" /> <SeekBar android:id="@+id/sb_green" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_margin="10dp"/> <SeekBar android:id="@+id/sb_blue" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_margin="10dp"/> <TextView android:id="@+id/tv_display" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="8dp" android:textSize="16sp" android:textColor="#333333"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_photo" android:layout_height="40dp" android:layout_width="0dp" android:layout_marginLeft="4dp" android:text="拍 照" android:textColor="#fff" android:background="@xml/btn_blue" android:layout_weight="1"/> <Button android:id="@+id/btn_picture" android:layout_height="40dp" android:layout_width="0dp" android:layout_marginLeft="4dp" android:layout_weight="1" android:textColor="#fff" android:layout_marginRight="4dp" android:background="@xml/btn_blue" android:text="相 冊(cè)"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:orientation="horizontal"> <Button android:id="@+id/btn_save" android:layout_height="40dp" android:layout_width="0dp" android:textColor="#fff" android:layout_marginLeft="4dp" android:layout_weight="1" android:background="@xml/btn_blue" android:text="保 存"/> <Button android:id="@+id/btn_origin" android:layout_height="40dp" android:layout_width="0dp" android:textColor="#fff" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" android:layout_weight="1" android:background="@xml/btn_blue" android:text="初始化"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:orientation="horizontal"> <Button android:id="@+id/btn_delete" android:layout_height="40dp" android:layout_width="0dp" android:layout_marginLeft="4dp" android:text="清除保存文件" android:textColor="#fff" android:background="@xml/btn_blue" android:layout_weight="1"/> <Button android:id="@+id/btn_see" android:layout_height="40dp" android:layout_width="0dp" android:layout_marginLeft="4dp" android:layout_weight="1" android:textColor="#fff" android:layout_marginRight="4dp" android:background="@xml/btn_blue" android:text="查看保存文件"/> </LinearLayout> </LinearLayout>

java

package com.example.mpaint; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentResolver; import android.content.ContentUris; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnSeekBarChangeListener,OnClickListener { private ImageView iv_change; private SeekBar sb_alpha, sb_red, sb_green, sb_blue; private TextView tv_dispaly; private float a = 0f, r = 0f, g = 0f, b = 0f; private String Path; private Button btn_photo, btn_picture, btn_save, btn_origin, btn_delete, btn_see; private Bitmap photo = null, photoorigin; private File mCurrentPhotoFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); // 新建圖片存儲(chǔ)的目錄 Path = Environment.getExternalStorageDirectory().toString() + "/mFilter"; File file = new File(Path); if (!file.exists()) { file.mkdir(); } iv_change = (ImageView) findViewById(R.id.iv_change); tv_dispaly = (TextView) findViewById(R.id.tv_display); tv_dispaly.setText("a: " + String.valueOf((a)) + "% " + "r: " + String.valueOf((r)) + " " + "g: " + String.valueOf((g)) + +" " + "b: " + String.valueOf((b))); // 從資源文件中獲得圖片 photoorigin = BitmapFactory.decodeResource(getResources(), R.drawable.origin); // 初始化進(jìn)度條 initSeek(); // 初始化按鈕 initButton(); } private void initButton() { btn_save = (Button) findViewById(R.id.btn_save); btn_save.setOnClickListener(this); btn_photo = (Button) findViewById(R.id.btn_photo); btn_photo.setOnClickListener(this); btn_picture = (Button) findViewById(R.id.btn_picture); btn_picture.setOnClickListener(this); btn_origin = (Button) findViewById(R.id.btn_origin); btn_origin.setOnClickListener(this); btn_delete = (Button) findViewById(R.id.btn_delete); btn_delete.setOnClickListener(this); btn_see = (Button) findViewById(R.id.btn_see); btn_see.setOnClickListener(this); } private void initSeek() { sb_alpha = (SeekBar) findViewById(R.id.sb_alpha); sb_alpha.setOnSeekBarChangeListener(this); sb_red = (SeekBar) findViewById(R.id.sb_red); sb_red.setOnSeekBarChangeListener(this); // 設(shè)置進(jìn)度條默許位置 sb_red.setProgress(50); sb_green = (SeekBar) findViewById(R.id.sb_green); sb_green.setOnSeekBarChangeListener(this); sb_green.setProgress(50); sb_blue = (SeekBar) findViewById(R.id.sb_blue); sb_blue.setOnSeekBarChangeListener(this); sb_blue.setProgress(50); } // 重寫OnSeekBarChangeListener @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { switch (seekBar.getId()) { case R.id.sb_alpha: a = (float) (progress / 100.0); break; case R.id.sb_red: r = (float) ((progress - 50) / 5.0); break; case R.id.sb_green: g = (float) ((progress - 50) / 5.0); break; case R.id.sb_blue: b = (float) ((progress - 50) / 5.0); break; default: break; } setArgb(1 - a, r + 1, g + 1, b + 1); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } public void setArgb(float alpha, float red, float green, float blue) { tv_dispaly.setText("a: " + String.valueOf((a * 100)) + "% " + "r: " + String.valueOf((r)) + " " + "g: " + String.valueOf((g)) + " " + "b: " + String.valueOf((b))); // 新建色彩矩陣 ColorMatrix mColorMatrix = new ColorMatrix(new float[] { red, 0, 0, 0, 0, 0, green, 0, 0, 0, 0, 0, blue, 0, 0, 0, 0, 0, alpha, 0 }); // 使用色彩矩陣過濾圖片 iv_change.getDrawable().setColorFilter( new ColorMatrixColorFilter(mColorMatrix)); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_origin: a = 0f; r = 0f; g = 0f; b = 0f; tv_dispaly.setText("a: " + String.valueOf((a)) + "% " + "r: " + String.valueOf((r)) + " " + "g: " + String.valueOf((g)) + " " + "b: " + String.valueOf((b))); sb_alpha.setProgress(0); sb_red.setProgress(50); sb_green.setProgress(50); sb_blue.setProgress(50); // 初始化圖片 if (photo != null) { iv_change.setBackgroundColor(Color.parseColor("#00000000")); iv_change.setImageBitmap(photo); } else { iv_change.setBackgroundColor(Color.parseColor("#00000000")); iv_change.setImageBitmap(photoorigin); } break; case R.id.btn_save: saveImageView(); break; case R.id.btn_photo: takephoto(); break; case R.id.btn_picture: takepicture(); break; case R.id.btn_see: see(); break; case R.id.btn_delete: new AlertDialog.Builder(MainActivity.this) .setTitle("提示信息") .setMessage("是不是刪除路徑" + Path + "下的所有文件" + "?") .setNegativeButton("確認(rèn)", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { delete(); dialog.dismiss(); // 判斷是不是完全刪除 File file = new File(Path); File[] childFile = file.listFiles(); if (childFile.length == 0) { Toast.makeText(MainActivity.this, "刪除成功", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(MainActivity.this, "刪除失敗", Toast.LENGTH_LONG) .show(); } } }) .setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).show(); break; default: break; } } // 查看文件 private void see() { Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); File file = new File(Path); intent.setDataAndType(Uri.fromFile(file), "*/*"); startActivityForResult(intent, 2); } private void delete() { File file = new File(Path); File[] childFile = file.listFiles(); for (File file2 : childFile) { file2.delete(); } } private void takepicture() { // 相冊(cè) Intent intentPick = new Intent(Intent.ACTION_PICK, null); intentPick.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intentPick, 0); } private void takephoto() { // 照相 mCurrentPhotoFile = new File(Path, "temp.jpg"); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile)); startActivityForResult(intent, 1); } private void saveImageView() { // 自定義文件名 final String path = Path + "/" + getNowDateTime("yyyyMMddHHmmss") + ".jpg"; new AlertDialog.Builder(MainActivity.this) .setTitle("提示信息") .setMessage("是不是將文件保存到" + Path + "?") .setNegativeButton("確認(rèn)", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 設(shè)置view可緩存.然后以bitmap獲得view的緩存 iv_change.setDrawingCacheEnabled(true); Bitmap photofinally = iv_change.getDrawingCache(); try { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(path)); // 將bitmap緊縮存儲(chǔ),100表示不緊縮 photofinally.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); bos = null; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } iv_change.setDrawingCacheEnabled(false); File file = new File(path); dialog.dismiss(); if (file.exists()) { Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "保存失敗", Toast.LENGTH_LONG).show(); } } }) .setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).show(); } public static String getNowDateTime(String format) { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat(format);// "yyyyMMdd" String dateString = formatter.format(currentTime); return dateString; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 0: if (data != null) { // 相冊(cè)u(píng)ri獲得 Uri uri = data.getData(); // 同享相機(jī)獲得的數(shù)據(jù) ContentResolver cr = this.getContentResolver(); try { // 回收bitmap if (photo != null && !photo.isRecycled()) { photo.recycle(); System.gc(); } // 將好;獲得的數(shù)據(jù)編碼成bitmap photo = BitmapFactory.decodeStream(cr.openInputStream(uri)); } catch (Exception e) { e.printStackTrace(); } iv_change.setBackgroundColor(Color.parseColor("#00000000")); iv_change.setImageBitmap(photo); } break; case 1: // resultCode=⑴為成功 if (resultCode == -1) { ContentResolver cr = getContentResolver(); Uri fileUri = Uri.fromFile(mCurrentPhotoFile); // 刷新單個(gè)文件 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, fileUri)); // 刷新sd卡 // sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, // dirUri)); try { if (photo != null && !photo.isRecycled()) { photo.recycle(); System.gc(); } // 通過uri讀取資源 photo = BitmapFactory.decodeStream(cr .openInputStream(fileUri)); } catch (Exception e) { e.printStackTrace(); } iv_change.setBackgroundColor(Color.parseColor("#00000000")); iv_change.setImageBitmap(photo); } break; case 2: break; default: break; } super.onActivityResult(requestCode, resultCode, data); } }

剪裁圖片

private void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 設(shè)置裁剪 intent.putExtra("crop", "true"); // aspectX aspectY 是寬高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪圖片寬高 intent.putExtra("outputX", 320); intent.putExtra("outputY", 320); //返回?cái)?shù)據(jù) intent.putExtra("return-data", true); startActivityForResult(intent, REQUEST_CODE_HEAD); } //處理onActivityResult private Bitmap saveImageView(String path, Intent data) throws Exception { Bitmap photo = null; //獲得綁定數(shù)據(jù)包 Bundle extras = data.getExtras(); if (extras != null) { //獲得綁定數(shù)據(jù) photo = extras.getParcelable("data"); BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(path)); photo.compress(Bitmap.CompressFormat.JPEG, 60, bos); bos.flush(); bos.close(); bos = null; } return photo; }
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 在线成人免费视频 | 久久亚洲伊人 | 久久大香伊人中文字幕 | 国内精品久久影视 | 日本一区二区成人教育 | 自拍偷拍1 | 你操综合 | 亚洲综合无码一区二区 | xh98hx国产免费 | 性欧美高清videosex | 亚洲综合在线视频 | 欧美日韩国产综合在线 | 9久热久爱免费精品视频在线观看 | 母狗求操| 国产精品久久久久一区二区 | 国产农村1级毛片 | 免费播放观看在线视频 | 日本高清中文字幕一区二区三区a | www.黄色网址| 亚洲欧美国产精品专区久久 | 名优写真一区二区在线 | 国产精品一区三区 | 久久一区二区免费播放 | 欧洲免费无线码二区5 | 中文字幕日本在线视频二区 | 一本久到久久亚洲综合 | 久久精品国产国语对白 | 亚洲国产成人久久综合区 | 武则天一级淫片免费放 | 福利视频美女国产精品 | www.99精品视频在线播放 | 精品国产精品久久一区免费式 | 亚洲精品乱无伦码 | 国产精品一国产精品免费 | www性| linode日本iphone强汉 | 国产精品欧美韩国日本久久 | 成人国产视频在线观看 | 免费aⅴ网站 | 动漫日本在线免费观看 | 狠狠操视频网 |