gpt4 book ai didi

java - Android相机应用程序: Outputting bitmap to second activity

转载 作者:行者123 更新时间:2023-12-01 11:51:56 25 4
gpt4 key购买 nike

我想在内部保存图像,然后在下一个 Activity 中显示它。这是相机 Activity (预览和一切工作正常,但是当我按下“捕获”按钮时,我被发送到第二个 Activity ,但它只是一个白屏......没有显示位图图像)。

主要相机 Activity :

public class MainActivity extends FragmentActivity {


private static final String TAG = "CameraActivity";

public static final int MEDIA_TYPE_IMAGE = 1;
private Camera mCamera;
private CameraPreview mPreview;
private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mContext = this;

if(checkCameraHardware(mContext)){
// Create an instance of Camera
mCamera = getCameraInstance();

// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);

// Add a listener to the Capture button
Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
}
}
);
}
}

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

@Override
public void onPictureTaken(final byte[] data, Camera camera) {

new AsyncTask<Void, Void, String>() {

@Override
protected String doInBackground(Void... params) {


try {
FileOutputStream fos = openFileOutput("img.jpg", Context.MODE_PRIVATE);
fos.write(data);
fos.close();
return "img.jpg";
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
return null;
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
return null;
}
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result != null){
Intent intent = new Intent(mContext, ImageDisplayActivity.class);
intent.putExtra(ImageDisplayActivity.KEY_PATH, "img.jpg");
startActivity(intent);
}
}

}.execute();


}
};
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
/** Create a File for saving an image or video */


}

这是第二个“显示图像” Activity :

public class ImageDisplayActivity extends FragmentActivity {

public static final String KEY_PATH = "img.jpg";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//final Bundle extras = getIntent().getExtras();
Intent intent = getIntent();
String path = getIntent().getStringExtra("EXTRA_FILE");

try {
java.io.FileInputStream in = this.openFileInput(path);
Bitmap bitmap = BitmapFactory.decodeStream(in);
ImageView mImg;
mImg = (ImageView) findViewById(R.id.image_displayer);
mImg.setRotation(90);
mImg.setImageBitmap(bitmap);
in.close();

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




}


private Bitmap decodeFile(File f){
Bitmap b = null;
try {
BitmapFactory.Options o = new BitmapFactory.Options();
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
BitmapFactory.Options o2 = new BitmapFactory.Options();
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
}
return b;
}

}

这是 LogCat:

02-26 00:56:05.905  12138-12138/com.commonsware.android.test1 W/System.err﹕ java.lang.NullPointerException

02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ContextImpl.makeFilename(ContextImpl.java:2456)

02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ContextImpl.openFileInput(ContextImpl.java:1072)

02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.content.ContextWrapper.openFileInput(ContextWrapper.java:180)

02-26 00:56:05.905 12138-12138/com.commonsware.android.test1 W/System.err﹕ at com.commonsware.android.test1.ImageDisplayActivity.onCreate(ImageDisplayActivity.java:33)

02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5372)

02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)

02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)

02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)

02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.access$700(ActivityThread.java:165)

02-26 00:56:05.915 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)

02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)

02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.os.Looper.loop(Looper.java:137)

02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5455)

02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)

02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-26 00:56:05.925 12138-12138/com.commonsware.android.test1 W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

最佳答案

使用ImageDisplayActivity.KEY_PATH作为key而不是EXTRA_FILE来获取从先前带有Intent的Activity传递的值:

String path = getIntent().getStringExtra(ImageDisplayActivity.KEY_PATH);

关于java - Android相机应用程序: Outputting bitmap to second activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28735532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com