Android 现在版本出到4.4.2了,这也带来了一些各个版本上通用的问题,比如拍照获取本地图片的路径之类。在android 4.4 以下的版本中,一些常用的获取本地图片的路径方法在4.4 以上有的时候不能够用了,所有需要对获取图片路径做个判断。
项目中用到的方法如下,留着下次自己备用了:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
@SuppressLint("NewApi")
public class PicActivity extends Activity {
//图片名称
private String picname ;
//图片路径
private String picpath ;
private ImageView img;
private Button button;
public static final String IMAGE_UNSPECIFIED = "image/*";
String sdcard_temp = Environment.getExternalStorageDirectory()
+ File.separator + "tmps.jpg";
Bitmap bitmap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addwithout);
// 初始化界面
init();
}
private void init() {
button = (Button) findViewById(R.id.button);
img = (ImageView) findViewById(R.id.addimg);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String[] items = { "相机拍照", "本地相册" };
new AlertDialog.Builder(WithoutActivity.this).setTitle("选择")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(sdcard_temp)));
startActivityForResult(intent, 1);
} else {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setType("image/*");
startActivityForResult(intent, 2);
}
}
}).show();
}
});
}
//响应图片设置按钮的处理方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = 8;
File file = new File(sdcard_temp);
picpath = file.getPath();//图片路径
picname = file.getName();//图片名称
System.out.println("拍照=======" + picpath + "," + picname);
if (file.exists()) {
Bitmap cameraBitmap = BitmapFactory.decodeFile(
sdcard_temp, bitmapOptions);
bitmap = cameraBitmap;
img.setImageBitmap(bitmap);
}
}
} else if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
/**
* android sdk4.4以上版本获取图片路径不一样,需要另外的处理方法
*/
if (android.os.Build.VERSION.SDK_INT >= 19) {
File pic = new File(uri2filePath(data.getData(),WithoutActivity.this));
picpath = pic.getPath();
System.out.println("本地图片=======" + picpath);
try {
Bitmap photo = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), Uri.fromFile(pic));
bitmap = photo;
if (photo != null) {
img.setImageBitmap(photo);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
Uri originalUri = data.getData(); //获得图片的uri
//判断是否是空
if (data != null && data.getData() != null) {
originalUri = data.getData();
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), originalUri);
//显得到bitmap图片
if (bitmap != null) {
img.setImageBitmap(bitmap);
}
//这里开始的第二部分,获取图片的路径:
String[] proj = {MediaStore.Images.Media.DATA};
//好像是android多媒体数据库的封装接口,具体的看Android文档
Cursor cursor = managedQuery(originalUri, proj, null, null, null);
//按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//将光标移至开头 ,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
//最后根据索引值获取图片路径
picpath = cursor.getString(column_index);
System.out.println("本地图片=======" + picpath );
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
// android 4.4 获取图片路径的方法
public static String uri2filePath(Uri uri, Activity activity) {
String path = "";
if (DocumentsContract.isDocumentUri(activity, uri)) {
String wholeID = DocumentsContract.getDocumentId(uri);
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = activity.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
new String[] { id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
path = cursor.getString(columnIndex);
}
cursor.close();
} else {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = activity.getContentResolver().query(uri,
projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index);
}
return path;
}
}
本文由 上海java培训 机构推荐阅读,更多精彩请浏览 上海it培训 官网。
项目中用到的方法如下,留着下次自己备用了:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
@SuppressLint("NewApi")
public class PicActivity extends Activity {
//图片名称
private String picname ;
//图片路径
private String picpath ;
private ImageView img;
private Button button;
public static final String IMAGE_UNSPECIFIED = "image/*";
String sdcard_temp = Environment.getExternalStorageDirectory()
+ File.separator + "tmps.jpg";
Bitmap bitmap = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addwithout);
// 初始化界面
init();
}
private void init() {
button = (Button) findViewById(R.id.button);
img = (ImageView) findViewById(R.id.addimg);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String[] items = { "相机拍照", "本地相册" };
new AlertDialog.Builder(WithoutActivity.this).setTitle("选择")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(sdcard_temp)));
startActivityForResult(intent, 1);
} else {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setType("image/*");
startActivityForResult(intent, 2);
}
}
}).show();
}
});
}
//响应图片设置按钮的处理方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = 8;
File file = new File(sdcard_temp);
picpath = file.getPath();//图片路径
picname = file.getName();//图片名称
System.out.println("拍照=======" + picpath + "," + picname);
if (file.exists()) {
Bitmap cameraBitmap = BitmapFactory.decodeFile(
sdcard_temp, bitmapOptions);
bitmap = cameraBitmap;
img.setImageBitmap(bitmap);
}
}
} else if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
/**
* android sdk4.4以上版本获取图片路径不一样,需要另外的处理方法
*/
if (android.os.Build.VERSION.SDK_INT >= 19) {
File pic = new File(uri2filePath(data.getData(),WithoutActivity.this));
picpath = pic.getPath();
System.out.println("本地图片=======" + picpath);
try {
Bitmap photo = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), Uri.fromFile(pic));
bitmap = photo;
if (photo != null) {
img.setImageBitmap(photo);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
Uri originalUri = data.getData(); //获得图片的uri
//判断是否是空
if (data != null && data.getData() != null) {
originalUri = data.getData();
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), originalUri);
//显得到bitmap图片
if (bitmap != null) {
img.setImageBitmap(bitmap);
}
//这里开始的第二部分,获取图片的路径:
String[] proj = {MediaStore.Images.Media.DATA};
//好像是android多媒体数据库的封装接口,具体的看Android文档
Cursor cursor = managedQuery(originalUri, proj, null, null, null);
//按我个人理解 这个是获得用户选择的图片的索引值
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//将光标移至开头 ,这个很重要,不小心很容易引起越界
cursor.moveToFirst();
//最后根据索引值获取图片路径
picpath = cursor.getString(column_index);
System.out.println("本地图片=======" + picpath );
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
// android 4.4 获取图片路径的方法
public static String uri2filePath(Uri uri, Activity activity) {
String path = "";
if (DocumentsContract.isDocumentUri(activity, uri)) {
String wholeID = DocumentsContract.getDocumentId(uri);
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = activity.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
new String[] { id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
path = cursor.getString(columnIndex);
}
cursor.close();
} else {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = activity.getContentResolver().query(uri,
projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
path = cursor.getString(column_index);
}
return path;
}
}
本文由 上海java培训 机构推荐阅读,更多精彩请浏览 上海it培训 官网。