Android 如何从图库中选择图像
我正在从Gallery中选择图像并在手机中进行裁剪,然后上传到服务器 当我测试其他设备时 无法将图像拾取到某些设备Android 如何从图库中选择图像,android,onactivityresult,Android,Onactivityresult,我正在从Gallery中选择图像并在手机中进行裁剪,然后上传到服务器 当我测试其他设备时 无法将图像拾取到某些设备 public void selectImageFromGallery() { String TEMP_PHOTO_FILE = "temporary_holder.jpg"; Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.Medi
public void selectImageFromGallery() {
String TEMP_PHOTO_FILE = "temporary_holder.jpg";
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent, PICK_IMAGE);
}
private Uri getTempUri() {
return Uri.fromFile(getTempFile());
}
private File getTempFile() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(),"temporary_holder.jpg");
try {
file.createNewFile();
} catch (IOException e) {}
return file;
} else {
return null;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode)
{
case PICK_IMAGE:
if (resultCode == RESULT_OK) {
if (imageReturnedIntent!=null) {
File tempFile = getTempFile();
String filePath= Environment.getExternalStorageDirectory()
+"/"+"temporary_holder.jpg";
System.out.println("path "+filePath);
decodeFile(filePath);
if (tempFile.exists()) tempFile.delete();
}
}
}
}
/**
* The method decodes the image file to avoid out of memory issues. Sets the
* selected image in to the ImageView.
*
* @param filePath
*/
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeFile(filePath, o2);
roundedImage=new RoundedImage(bitmap);
image.setImageDrawable(roundedImage);
}
public void选择ImagefromGallery(){
字符串TEMP\u PHOTO\u FILE=“temporary\u holder.jpg”;
Intent PHOTOCKERRINTENT=新Intent(Intent.ACTION\U PICK,
android.provider.MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);
photopickerinent.setType(“image/*”);
PhotoPickerinent.putExtra(“作物”、“真实”);
photopickerinent.putExtra(MediaStore.EXTRA_输出,getTempUri());
photoPickerIntent.putExtra(“outputFormat”,Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(光采集器内容、拾取图像);
}
私有Uri getTempUri(){
返回Uri.fromFile(getTempFile());
}
私有文件getTempFile(){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File File=新文件(Environment.getExternalStorageDirectory(),“temporary_holder.jpg”);
试一试{
createNewFile();
}捕获(IOE){}
返回文件;
}否则{
返回null;
}
}
@凌驾
ActivityResult上受保护的void(int请求代码、int结果代码、,
意图(返回内容){
super.onActivityResult(请求代码、结果代码、图像返回内容);
开关(请求代码)
{
案例选取图像:
如果(resultCode==RESULT_OK){
if(imageReturnedIntent!=null){
File tempFile=getTempFile();
字符串filePath=Environment.getExternalStorageDirectory()
+“/”+“临时持有人.jpg”;
System.out.println(“路径”+文件路径);
解码文件(文件路径);
如果(tempFile.exists())tempFile.delete();
}
}
}
}
/**
*该方法解码图像文件以避免内存不足问题。设定
*将选定的图像导入到ImageView。
*
*@param文件路径
*/
公共文件(字符串文件路径){
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码文件(文件路径,o);
//我们要扩展到的新尺寸
所需的最终int_SIZE=1024;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
如果(宽度\u tmp<要求的\u尺寸和高度\u tmp<要求的\u尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
比例*=2;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
位图=BitmapFactory.decodeFile(文件路径,o2);
roundedImage=新的roundedImage(位图);
image.setImageDrawable(roundeImage);
}
这是我的密码
请告诉我哪里做错了
如果Sd卡在无空间中不可用,我想删除以在Sd卡上创建新文件
应用程序崩溃//初始化变量
int select_photo = 1;
//使用此方法启动intent
void Profile_Image_Pick() {
Intent in = new Intent(Intent.ACTION_PICK);
in.setType("image/*");
startActivityForResult(in, select_photo);
}
//现在是ActivityforResult
@Override
protected void onActivityResult(int requestCode, int resultCode,
super.onActivityResult(requestCode, resultCode, imagereturnintent);
switch (requestCode) {
case select_photo:
if (resultCode == RESULT_OK) {
final Uri imageuri = imagereturnintent.getData();
final InputStream imageStream = getContentResolver()
.openInputStream(imageUri);
bitmap = BitmapFactory.decodeStream(imageStream);
Bitmap bt = Bitmap.createScaledBitmap(bitmap, 70, 70, false);
profile_image.setImageBitmap(bt);
}
}
}
//对于检查SD卡是否存在,您可以使用
if(android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
//sdcard present
else
//not present
最后调用start方法。
希望能有所帮助。当你从过时/不完整的教程中复制/粘贴代码而不了解代码的作用时,可能会发生类似的情况…@2Dee..我是Android教程的新手,教程是从gallery中选择图像并上传到服务器