我使用的代码来自示例here。拍摄照片,然后在ImageView中加载并不总是正常工作
我的问题是,有时图像不加载到ImageView。我刚刚注意到了这种行为。我发现如果通过代码进行调试,它可以在100%的时间内正常工作。
我的假设是,图像文件有时不会及时创建,以便在onActivityResult
中可用。有没有人遇到过这个问题?如何补偿的建议?
我使用的代码来自示例here。拍摄照片,然后在ImageView中加载并不总是正常工作
我的问题是,有时图像不加载到ImageView。我刚刚注意到了这种行为。我发现如果通过代码进行调试,它可以在100%的时间内正常工作。
我的假设是,图像文件有时不会及时创建,以便在onActivityResult
中可用。有没有人遇到过这个问题?如何补偿的建议?
可能是移动处理速度问题!不能确切地说,但我试过这种方式,它总是像魅力一样工作!
public class LaunchCamera extends Activity {
ImageView imVCature_pic;
Button btnCapture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_camera);
initializeControls();
}
private void initializeControls() {
imVCature_pic=(ImageView)findViewById(R.id.imVCature_pic);
btnCapture=(Button)findViewById(R.id.btnCapture);
btnCapture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* create an instance of intent
* pass action android.media.action.IMAGE_CAPTURE
* as argument to launch camera
*/
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
/*create instance of File with name img.jpg*/
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
/*put uri as extra in intent object*/
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
/*start activity for result pass intent as argument and request code */
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//if request code is same we pass as argument in startActivityForResult
if(requestCode==1){
//create instance of File with same name we created before to get image from storage
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
//get bitmap from path with size of
imVCature_pic.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 600, 450));
}
}
public static Bitmap decodeSampledBitmapFromFile(String path,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
//Query bitmap without allocating memory
options.inJustDecodeBounds = true;
//decode file from path
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
//decode according to configuration or according best match
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight) {
inSampleSize = Math.round((float)height/(float)reqHeight);
}
int expectedWidth = width/inSampleSize;
if (expectedWidth > reqWidth) {
//if(Math.round((float)width/(float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width/(float)reqWidth);
}
//if value is greater than 1,sub sample the original image
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
}
感谢您花时间研究它。我无法再让问题再次出现,所以我可能会无意中修复它,但是我的手机也刚刚得到更新,因此很难说出发生了什么。享受赏金:) – Jack 2014-09-29 13:57:25
你可以发布一些代码吗?很难知道没有它会导致问题的原因! – nyx 2014-09-26 09:40:44