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"