我需要一些帮助。问题是我在读卡器上做了一个应用程序,需要使用相机功能拍照。但是如何让我的应用程序能够查看我保存的多张图片而无需退出应用程序并转到图库?下面是我使用的相机源代码..谢谢
`
CameraMainActivity.Java
private static final int REQUEST_CODE = 1;
private static final int CAMERA_PIC_REQUEST = 1337;
private Bitmap bitmap;
private ImageView imageView;
private Button save, capture;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_main);
imageView = (ImageView) findViewById(R.id.result);
save = (Button) findViewById(R.id.save);
save.setOnClickListener(this);
capture = (Button) findViewById(R.id.capture);
capture.setOnClickListener(this);
}
public void captureImage() {
// Capture image from camera
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
public void pickImage() {
// To pick a image from file system
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CODE) {
try {
// We need to recycle unused bitmaps
if (bitmap != null) {
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(
data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == CAMERA_PIC_REQUEST) {
if (bitmap != null) {
bitmap.recycle();
}
bitmap = (Bitmap) data.getExtras().get("data");
if (data.getExtras().get("data") == null)
Toast.makeText(getApplicationContext(),
"No image returned", Toast.LENGTH_LONG).show();
else
imageView.setImageBitmap(bitmap);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_camera_main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == save) {
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "test.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(),
"Image saved with success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
} else if (v == capture) {
captureImage();
}
}
}
我正在使用按钮启动相机
主要的.xml
<Button
android:id="@+id/takeviewpic"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Take/View Pic" />
安卓清单
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.flash"
android:required="false" />
<uses-feature android:name="android.hardware.screen.landscape" />
<uses-feature
android:name="android.hardware.wifi"
android:required="false" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-sdk android:minSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name="sp.com.Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="main" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".NameCardList" >
</activity>
<activity android:name=".DetailForm" >
</activity>
<activity android:name=".EditPreferences" >
</activity>
<activity android:name=".NameCardMap" >
</activity>
<activity android:name=".SmsReceiver" >
</activity>
<activity
android:name=".CameraMainActivity"
android:label="@string/app_name"
android:clearTaskOnLaunch="true"
android:configChanges="orientation|keyboardHidden"
android:stateNotNeeded="true"
/>
</application>
`