android調用相冊
① Android怎麼調用系統相冊將選中的圖片設置為布局的背景,有沒有案例或源碼
Android中調用系統相機來拍攝照片的代碼,如下:
1、首先設置Uri獲取判斷以及相機請求Code
public final int TYPE_TAKE_PHOTO = 1;//Uri獲取類型判斷
public final int CODE_TAKE_PHOTO = 1;//相機RequestCode
2、調起系統相機
Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoUri = getMediaFileUri(TYPE_TAKE_PHOTO);
takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takeIntent, CODE_TAKE_PHOTO);
3、封裝獲取Uri代碼
public Uri getMediaFileUri(int type){
File mediaStorageDir = new File(Environment.(Environment.DIRECTORY_PICTURES), "相冊名字");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
//創建Media File
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == TYPE_TAKE_PHOTO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
} else {
return null;
}
return Uri.fromFile(mediaFile);
}
4、相機拍照完畢後獲取返回數據,並在頁面顯示照片
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CODE_TAKE_PHOTO:
if (resultCode == RESULT_OK) {
if (data != null) {
if (data.hasExtra("data")) {
Log.i("URI", "data is not null");
Bitmap bitmap = data.getParcelableExtra("data");
imageView.setImageBitmap(bitmap);//imageView即為當前頁面需要展示照片的控制項,可替換
}
} else {
Log.i("URI", "Data is null");
Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());
imageView.setImageBitmap(bitmap);//imageView即為當前頁面需要展示照片的控制項,可替換
}
}
break;
}
}
特殊:
一般情況,以上代碼在Android7.0以下,也就是api<24時,運行是沒有任何問題的。可是當targetSdkVersion變成24及其以上並且在android7.0(及以上版本)系統運行時,會拋出異常:FileUriExposedException。
② android怎麼調用系統自帶的圖庫打開指定目錄的相冊
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//完成照相後回調用此方法
super.onActivityResult(requestCode, resultCode, data);
case 1:
switch (resultCode) {
case Activity.RESULT_OK://照相完成點擊確定
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測sd是否可用
Log.v("TestFile", "SD card is not avaiable/writeable right now.");
return; }
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 獲取相機返回的數據,並轉換為Bitmap圖片格式
FileOutputStream b = null;
File file = new File("/sdcard/pk4fun/");
file.mkdirs();// 創建文件夾,名稱為pk4fun // 照片的命名,目標文件夾下,以當前時間數字串為名稱,即可確保每張照片名稱不相同。網上流傳的其他Demo這里的照片名稱都寫死了,則會發生無論拍照多少張,後一張總會把前一張照片覆蓋。細心的同學還可以設置這個字元串,比如加上「IMG」字樣等;然後就會發現sd卡中myimage這個文件夾下,會保存剛剛調用相機拍出來的照片,照片名稱不會重復。
String str = null;
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");// 獲取當前時間,進一步轉化為字元串
date = new Date(resultCode);
str = format.format(date);
String fileName = "/sdcard/myImage/" + str + ".jpg";
sendBroadcast(fileName);
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數據寫入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
} break;
case Activity.RESULT_CANCELED:// 取消
break;
}
break;
case 2:
switch (resultCode) {
case Activity.RESULT_OK: {
Uri uri = data.getData();
Cursor cursor = mActivity.getContentResolver().query(uri, null,
null, null, null);
cursor.moveToFirst();
String imgNo = cursor.getString(0); // 圖片編號
String imgPath = cursor.getString(1); // 圖片文件路徑
String imgSize = cursor.getString(2); // 圖片大小
String imgName = cursor.getString(3); // 圖片文件名
cursor.close();
// Options options = new BitmapFactory.Options();
// options.inJustDecodeBounds = false;
// options.inSampleSize = 10;
// Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
}
break;
case Activity.RESULT_CANCELED:// 取消
break;
}
break;
}
③ android怎樣調用系統相冊
使用系統自帶的圖片瀏覽器應用程序Gallery可以打開一張指定的圖片,//使用IntentIntent intent = new Intent(Intent.ACTION_VIEW);//Uri mUri = Uri.parse("file://" + picFile.getPath());Android3.0以後最好不要通過該方法,存在一些小Bugintent.setDataAndType(Uri.fromFile(picFile), "image/*");startActivity(intent)
④ android系統如何調用自帶的相機相冊
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="gallery"
android:text="獲取圖庫圖片" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="camera"
android:text="拍照獲取圖片" />
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
//------------------我的MainActivity --------------也很簡單--------------------------
package tackpicture.bwie.com.tackpicture;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private ImageView iv_image;
private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 從相冊中選擇
private static final int PHOTO_REQUEST_CUT = 3;// 結果
/* 頭像名稱 */
private static final String PHOTO_FILE_NAME = "temp_photo.jpg";
private File tempFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控制項
iv_image = (ImageView) findViewById(R.id.iv_image);
}
//圖庫
public void camera(View view) {
// 激活系統圖庫,選擇一張圖片
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
// 開啟一個帶有返回值的Activity,請求碼為PHOTO_REQUEST_GALLERY
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
//相機
public void gallery(View view) {
// 激活相機
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// 判斷存儲卡是否可以用,可用進行存儲
if (hasSdcard()) {
tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME);
// 從文件中創建uri
Uri uri = Uri.fromFile(tempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
// 開啟一個帶有返回值的Activity,請求碼為PHOTO_REQUEST_CAREMA
startActivityForResult(intent, PHOTO_REQUEST_CAREMA);
}
/*
* 剪切圖片
*/
private void crop(Uri uri) {
// 裁剪圖片意圖
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪後輸出圖片的尺寸大小
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("outputFormat", "JPEG");// 圖片格式
intent.putExtra("noFaceDetection", true);// 取消人臉識別
intent.putExtra("return-data", true);
// 開啟一個帶有返回值的Activity,請求碼為PHOTO_REQUEST_CUT
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
/*
* 判斷sdcard是否被掛載
*/
private boolean hasSdcard() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_REQUEST_GALLERY) {
// 從相冊返回的數據
if (data != null) {
// 得到圖片的全路徑
Uri uri = data.getData();
crop(uri);
}
} else if (requestCode == PHOTO_REQUEST_CAREMA) {
// 從相機返回的數據
if (hasSdcard()) {
crop(Uri.fromFile(tempFile));
} else {
Toast.makeText(MainActivity.this, "未找到存儲卡,無法存儲照片!", 0).show();
}
} else if (requestCode == PHOTO_REQUEST_CUT) {
// 從剪切圖片返回的數據
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");
this.iv_image.setImageBitmap(bitmap);
}
try {
// 將臨時文件刪除
tempFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
⑤ 安卓怎麼根據圖片路徑調用系統相冊查看這個圖片
//使用Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
//Uri mUri = Uri.parse("file://" + picFile.getPath());Android3.0以後最好不要通過該方法,存在一些小Bug
intent.setDataAndType(Uri.fromFile(picFile), "image/*");
startActivity(intent);
⑥ android 怎麼調取手機相冊
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(i, 11);
⑦ android 調用相冊的圖片 圖片無法找到 路徑應該怎麼寫
先引入Environment類,然後調用getExternalStorageDirectory()函數獲得sd卡路徑,然後連接在你的filename前面,應該就可以解決.
具體:
import android.os.Environment;
......
File f = new File(Environment.getExternalStorageDirectory() + fileName);
其他用到fileName的地方類似.
另外,讀寫sd卡需要在AndroidManifest.xml文件內加入許可權,具體:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
⑧ 如何調用android的拍照或本地相冊選取,然後再實現相片上傳伺服器
首先是拍照:使用Intent即可,
[java] view plainprint?
01.final String start = Environment.getExternalStorageState();
02.private static final String PHOTOPATH = "/photo/";
03.
04.if(start.equals(Environment.MEDIA_MOUNTED)){
05.Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
06.File file = new File(Environment.getExternalStorageDirectory()+PHOTOPATH);
07.if(!file.exists()){
08.file.mkdirs();
09.}
10.tempphontname = System.currentTimeMillis()+".jpg";
11.buffer.append(Environment.getExternalStorageDirectory()+PHOTOPATH).append(tempphontname);
12.intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(buffer.toString())));
13.startActivityForResult(intent, 1);
14.}
final String start = Environment.getExternalStorageState();
private static final String PHOTOPATH = "/photo/";
if(start.equals(Environment.MEDIA_MOUNTED)){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory()+PHOTOPATH);
if(!file.exists()){
file.mkdirs();
}
tempphontname = System.currentTimeMillis()+".jpg";
buffer.append(Environment.getExternalStorageDirectory()+PHOTOPATH).append(tempphontname);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(buffer.toString())));
startActivityForResult(intent, 1);
}
其次是從本地相冊選:依舊是Intent.
如下代碼:
[java] view plainprint?
01.if(start.equals(Environment.MEDIA_MOUNTED)){
02. Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
03. getImage.addCategory(Intent.CATEGORY_OPENABLE);
04. getImage.setType("image/jpeg");
05. startActivityForResult(getImage, 0);
06.}
if(start.equals(Environment.MEDIA_MOUNTED)){
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/jpeg");
startActivityForResult(getImage, 0);
}
接下來是主要的:因為調用完系統的方法後,回返回到回調方法onActivityResult(int, int, Intent)中,
在裡面進行主要的照片上傳伺服器的操作,
見代碼:
[java] view plainprint?
01.@Override
02. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
03. ContentResolver resolver = getContentResolver();
04. if(requestCode==1)//
05. {
06. if(resultCode==Activity.RESULT_OK)
07. {
08. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
09. {
10.ew Thread(new Runnable()
11. {
12. @Override
13. public void run() {
14.//進行上傳操作
15.}
16.}.start();
轉載
⑨ Android 如何調用相機或相冊,設置不同兩張圖片在兩個圖片控制項上 求代碼
遇到android項目導入出現後重復空包等錯誤,往往是導入的java編譯級別有關,點擊項目properties->
java Compiler ->修改Compiler compliance level 一般是1.6。
修改完成clean項目之後,往往還會遇到:
gen already exists but is not a source folder. Convert to a source folder or rename it的錯誤。
遇到這個問題的解決方法:
1. 右鍵點擊工程,選擇 "Properties"
2. 選擇左邊的 "Java Build Path"
3. 打開 "Source" 標簽面板
4. 點擊 "Add Folder..."
5. 勾選 "gen" 文件夾,點擊OK,點擊YES,再點擊OK
6. 最後右鍵點擊工程,選擇 "Andriod Tools" 裡面的 "Fix Project Properties"