【项目中】Android 小米红米手机无法读取照片图片路径的问题

前两天在项目中实现了可本地选择照片的功能,今天同事告诉我他的小米手机没有办法选择照片,点击选中的照片后总是无法将照片传入到应用内,而在选择图片页面选择“本地”,就可以成功选择照片和图片,至于原因还不得而知,其他品牌手机暂时没有这个问题。最后经过查找资料,已成功解决问题,解决方法如下:


Android程序内,从本地文件夹读取图片的功能实现,如下:

 private void openAlbum(int number) {
		if (image_path.size() == number) {
			Intent intent = new Intent(Intent.ACTION_PICK,
					android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
			startActivityForResult(intent, RESULT_CANCELED);
		}

	}


当我们选择一个图片以后,在onActivityResult中,我们用一下代码获取选择的图片路径:

Uri uri = data.getData();  
String path = uri.getPath();  


在这段代码中,使用小米手机的话,就会发现得到的path这个路径值,不是我们图片在手机上的路径,暂时还不明白是个什么值,如果根据这个path去获取图片会得到一个null值。

解决方案:在onActivityResult中,按照如下的方式,就可以在所有的手机上使用,不会出问题。

if (data != null) {    
    Uri uri = data.getData();    
    if (!TextUtils.isEmpty(uri.getAuthority())) {    
        Cursor cursor = getContentResolver().query(uri,  
                new String[] { MediaStore.Images.Media.DATA },null, null, null);    
        if (null == cursor) {    
            Toast.makeText(this, "图片没找到", Toast.LENGTH_SHORT).show();    
            return;    
        }    
        cursor.moveToFirst();    
        path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));  
        cursor.close();    
    } else {    
        path = uri.getPath();    
    }    
}else{    
    Toast.makeText(this, "图片没找到", Toast.LENGTH_SHORT).show();    
    return;    
}   
这个path就是我们要的图片路径,我们就可以用它去返回我们选择的照片。

完整代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			try {
			if (data != null) {    
			    Uri uri = data.getData();    
			    if (!TextUtils.isEmpty(uri.getAuthority())) {    
			        Cursor cursor = getContentResolver().query(uri,  
			                new String[] { MediaStore.Images.Media.DATA },null, null, null);    
			        if (null == cursor) {    
			            Toast.makeText(this, "图片没找到", Toast.LENGTH_SHORT).show();    
			            return;    
			        }    
			        cursor.moveToFirst();    
			        path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));  
			        cursor.close();    
			    } else {    
			        path = uri.getPath();    
			    }    
			}else{    
			    Toast.makeText(this, "图片没找到", Toast.LENGTH_SHORT).show();    
			    return;    
			}   
					Bitmap bmp = BitmapUtils.getimage(path);
					Drawable drawable = new BitmapDrawable(bmp);
					zooDrawable.add(drawable);

					if (BitmapUtils.isAvaliable()) {
						image_number++;
						BitmapUtils.saveBitmapToFile(bmp, BitmapUtils.SD_PATH + "/AITGou/" + image_number + ".jpg");
						image_path.add(BitmapUtils.SD_PATH + "/AITGou/" + image_number + ".jpg");
						Log.e("image_path", BitmapUtils.SD_PATH + "/AITGou/" + image_number + ".jpg");
					} else {
						image_path.add(path);
					}

					switch (image_path.size()) {
					case 1:
						imageview[0].setBackground(drawable);
						textview[0].setVisibility(View.VISIBLE);
						framelayout[1].setVisibility(View.VISIBLE);
						imageview[1].setVisibility(View.VISIBLE);
						imageview[1].setBackgroundResource(R.drawable.product_tagging_add);
						break;
					case 2:
						imageview[1].setBackground(drawable);
						textview[1].setVisibility(View.VISIBLE);
						framelayout[2].setVisibility(View.VISIBLE);
						imageview[2].setVisibility(View.VISIBLE);
						imageview[2].setBackgroundResource(R.drawable.product_tagging_add);
						break;
					case 3:
						imageview[2].setBackground(drawable);
						textview[2].setVisibility(View.VISIBLE);
						framelayout[3].setVisibility(View.VISIBLE);
						imageview[3].setVisibility(View.VISIBLE);
						imageview[3].setBackgroundResource(R.drawable.product_tagging_add);
						break;
					case 4:
						imageview[3].setBackground(drawable);
						textview[3].setVisibility(View.VISIBLE);
						framelayout[4].setVisibility(View.VISIBLE);
						imageview[4].setVisibility(View.VISIBLE);
						imageview[4].setBackgroundResource(R.drawable.product_tagging_add);
						project_edit_ll.setVisibility(View.VISIBLE);
						break;
					case 5:
						imageview[4].setBackground(drawable);
						textview[4].setVisibility(View.VISIBLE);
						framelayout[5].setVisibility(View.VISIBLE);
						imageview[5].setVisibility(View.VISIBLE);
						imageview[5].setBackgroundResource(R.drawable.product_tagging_add);
						break;
					case 6:
						imageview[5].setBackground(drawable);
						textview[5].setVisibility(View.VISIBLE);
						framelayout[6].setVisibility(View.VISIBLE);
						imageview[6].setVisibility(View.VISIBLE);
						imageview[6].setBackgroundResource(R.drawable.product_tagging_add);
						break;
					case 7:
						imageview[6].setBackground(drawable);
						textview[6].setVisibility(View.VISIBLE);
						framelayout[7].setVisibility(View.VISIBLE);
						imageview[7].setVisibility(View.VISIBLE);
						imageview[7].setBackgroundResource(R.drawable.product_tagging_add);
						break;
					case 8:
						imageview[7].setBackground(drawable);
						textview[7].setVisibility(View.VISIBLE);
						break;
					default:
						break;
					}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}








你可能感兴趣的:(android,图片,error,项目中,图片,android,小米手机,path,error)