折腾了三个多小时,程序无Bug,但是就是打不开照片。。。
完全照着郭老师的代码敲得。。。
版本不一样,想打开郭老师源代码,居然失败。。。
还需要进一步折腾,
吃点饭,
回来继续。
=============================
把郭老师的代码粘过来,果然能用。。。
实在找不到不一样的地方,先放放吧,将来会调试了在研究。
郭老师代码:
1 package com.example.cameraalbumtest; 2 3 import android.Manifest; 4 import android.annotation.TargetApi; 5 import android.content.ContentUris; 6 import android.content.Intent; 7 import android.content.pm.PackageManager; 8 import android.database.Cursor; 9 import android.graphics.Bitmap; 10 import android.graphics.BitmapFactory; 11 import android.net.Uri; 12 import android.os.Build; 13 import android.os.Bundle; 14 import android.os.Environment; 15 import android.provider.DocumentsContract; 16 import android.provider.MediaStore; 17 import android.support.annotation.NonNull; 18 import android.support.v4.app.ActivityCompat; 19 import android.support.v4.content.ContextCompat; 20 import android.support.v4.content.FileProvider; 21 import android.support.v7.app.AppCompatActivity; 22 import android.util.Log; 23 import android.view.View; 24 import android.widget.Button; 25 import android.widget.ImageView; 26 import android.widget.Toast; 27 28 import java.io.File; 29 import java.io.FileNotFoundException; 30 import java.io.IOException; 31 32 public class MainActivity extends AppCompatActivity { 33 public static final int TAKE_PHOTO = 1; 34 35 public static final int CHOOSE_PHOTO = 2; 36 37 private ImageView picture; 38 39 private Uri imageUri; 40 41 @Override 42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.activity_main); 45 Button takePhoto = (Button) findViewById(R.id.take_photo); 46 Button chooseFromAlbum = (Button) findViewById(R.id.choose_from_album); 47 picture = (ImageView) findViewById(R.id.picture); 48 takePhoto.setOnClickListener(new View.OnClickListener() { 49 @Override 50 public void onClick(View v) { 51 // 创建File对象,用于存储拍照后的图片 52 File outputImage = new File(getExternalCacheDir(), "output_image.jpg"); 53 try { 54 if (outputImage.exists()) { 55 outputImage.delete(); 56 } 57 outputImage.createNewFile(); 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } 61 if (Build.VERSION.SDK_INT < 24) { 62 imageUri = Uri.fromFile(outputImage); 63 } else { 64 imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.cameraalbumtest.fileprovider", outputImage); 65 } 66 // 启动相机程序 67 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 68 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); 69 startActivityForResult(intent, TAKE_PHOTO); 70 } 71 }); 72 chooseFromAlbum.setOnClickListener(new View.OnClickListener() { 73 @Override 74 public void onClick(View v) { 75 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 76 ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission. WRITE_EXTERNAL_STORAGE }, 1); 77 } else { 78 openAlbum(); 79 } 80 } 81 }); 82 } 83 84 private void openAlbum() { 85 Intent intent = new Intent("android.intent.action.GET_CONTENT"); 86 intent.setType("image/*"); 87 startActivityForResult(intent, CHOOSE_PHOTO); // 打开相册 88 } 89 90 @Override 91 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 92 switch (requestCode) { 93 case 1: 94 if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 95 openAlbum(); 96 } else { 97 Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show(); 98 } 99 break; 100 default: 101 } 102 } 103 104 @Override 105 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 106 switch (requestCode) { 107 case TAKE_PHOTO: 108 if (resultCode == RESULT_OK) { 109 try { 110 // 将拍摄的照片显示出来 111 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); 112 picture.setImageBitmap(bitmap); 113 } catch (Exception e) { 114 e.printStackTrace(); 115 } 116 } 117 break; 118 case CHOOSE_PHOTO: 119 if (resultCode == RESULT_OK) { 120 // 判断手机系统版本号 121 if (Build.VERSION.SDK_INT >= 19) { 122 // 4.4及以上系统使用这个方法处理图片 123 handleImageOnKitKat(data); 124 } else { 125 // 4.4以下系统使用这个方法处理图片 126 handleImageBeforeKitKat(data); 127 } 128 } 129 break; 130 default: 131 break; 132 } 133 } 134 135 @TargetApi(19) 136 private void handleImageOnKitKat(Intent data) { 137 String imagePath = null; 138 Uri uri = data.getData(); 139 Log.d("TAG", "handleImageOnKitKat: uri is " + uri); 140 if (DocumentsContract.isDocumentUri(this, uri)) { 141 // 如果是document类型的Uri,则通过document id处理 142 String docId = DocumentsContract.getDocumentId(uri); 143 if("com.android.providers.media.documents".equals(uri.getAuthority())) { 144 String id = docId.split(":")[1]; // 解析出数字格式的id 145 String selection = MediaStore.Images.Media._ID + "=" + id; 146 imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); 147 } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { 148 Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); 149 imagePath = getImagePath(contentUri, null); 150 } 151 } else if ("content".equalsIgnoreCase(uri.getScheme())) { 152 // 如果是content类型的Uri,则使用普通方式处理 153 imagePath = getImagePath(uri, null); 154 } else if ("file".equalsIgnoreCase(uri.getScheme())) { 155 // 如果是file类型的Uri,直接获取图片路径即可 156 imagePath = uri.getPath(); 157 } 158 displayImage(imagePath); // 根据图片路径显示图片 159 } 160 161 private void handleImageBeforeKitKat(Intent data) { 162 Uri uri = data.getData(); 163 String imagePath = getImagePath(uri, null); 164 displayImage(imagePath); 165 } 166 167 private String getImagePath(Uri uri, String selection) { 168 String path = null; 169 // 通过Uri和selection来获取真实的图片路径 170 Cursor cursor = getContentResolver().query(uri, null, selection, null, null); 171 if (cursor != null) { 172 if (cursor.moveToFirst()) { 173 path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); 174 } 175 cursor.close(); 176 } 177 return path; 178 } 179 180 private void displayImage(String imagePath) { 181 if (imagePath != null) { 182 Bitmap bitmap = BitmapFactory.decodeFile(imagePath); 183 picture.setImageBitmap(bitmap); 184 } else { 185 Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show(); 186 } 187 } 188 189 }