我正在编写一个应用程序,我需要放置一个图像视图,用户必须通过单击它来加载图像。单击“我要”后,用户可以选择他是打算从手机本身加载存储的图像还是从它的相机拍摄新照片。
这个问题可能是多余的,但这里揭示的类似问题/问题几乎都没有达到我想要做的事情。
Ps 我正在使用安装了 Eclipse 4.2 (JUNO) SDK 的 Android API15。
这是主要活动的片段代码,它给了我一个错误:
package test.imgbyte.conerter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.net.Uri;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
public class FindImgPathActivity extends Activity
{
private Uri mImageCaptureUri;
static final int CAMERA_PIC_REQUEST = 1337;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imgfilepath);
Button camera = (Button) findViewById(R.id.btnLoad);
camera.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imgLoaded);
image.setImageBitmap(thumbnail);
String pathToImage = mImageCaptureUri.getPath();
// pathToImage is a path you need.
// If image file is not in there,
// you can save it yourself manually with this code:
File file = new File(pathToImage);
FileOutputStream fOut;
try
{
fOut = new FileOutputStream(file);
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // You can choose any format you want
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
}
我从 LogCat 得到的错误是这样的:
11-05 19:23:11.777: E/AndroidRuntime(1206): FATAL EXCEPTION: main
11-05 19:23:11.777: E/AndroidRuntime(1206): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=Intent { act=inline-data (has extras) }} to activity {test.imgbyte.conerter/test.imgbyte.conerter.FindImgPathActivity}: java.lang.NullPointerException