【发布时间】:2014-08-31 01:57:57
【问题描述】:
我是安卓开发的新手。我一直在尝试访问相机,拍摄照片,然后返回我的活动并将其显示在 ImageView 上。
这是我的活动代码(很简单,就是这么做的)
public class Profile extends Activity {
private static final int CAMERA_REQUEST = 1337;
private ImageView imagePhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
imagePhoto = (ImageView) findViewById(R.id.imagePhoto);
ImageView photoButton = (ImageView) this.findViewById(R.id.camera);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.imagePhoto);
image.setImageBitmap(thumbnail);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我做错了什么?我搜索并尝试了很多代码。当我点击按钮时,相机出现,我拍照,然后我返回活动,但照片的缩略图没有显示在 ImageView“imagePhoto”中。
这是 imagePhoto 的布局:
<ImageView
android:id="@+id/imagePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/welcome"
android:layout_marginRight="50dp"
android:layout_marginTop="82dp"
android:layout_toLeftOf="@+id/camera"
android:src="@drawable/add_photo" /> `/* It has another photo, because I was trying to see if it changes */
这也在我的清单中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
我尝试了很多代码!提前致谢。我使用的是三星设备。
【问题讨论】:
-
具体是什么问题?检索到的位图是否为空?
-
@valbertos 问题是缩略图没有显示我拍摄的图像。如果位图为空,我现在怎么办?
-
使用 IDE 或根据条件打印结果,如下所示:if (thumbnail == null){Log.d("bitmap", "Bitmap is null!")}
-
我把它放了,控制台中什么也没有显示。 @valberos
-
所以,Bitmap 不为 null :) 尝试删除 XML 中的可绘制集,它可能与 Bitmap 重叠。