这是我的下面的代码是浏览按钮并从相机拍摄图像并在屏幕上显示是好的但我想循环这样做3次以编程方式我的意思是i = 1到2是浏览按钮拍摄图像和显示所以屏幕显示3按钮3图像使用for循环我该怎么做?
public class MainActivity extends Activity {
public static Bitmap yourSelectedImage = null;
private final int CAMERA_PICTURE = 1;
private final int GALLERY_PICTURE = 2;
private Intent pictureActionIntent = null;
public final String SDCARD_ROOT_PATH =
Environment.getExternalStorageDirectory().getAbsolutePath();
public final String SAVE_PATH_IN_SDCARD = "/myFolder23/";
public final String IMAGE_CAPTURE_NAME
="imgtemp"+System.currentTimeMillis()+".jpeg";
ImageView imageView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout btnLO = new LinearLayout(this);
LinearLayout.LayoutParams paramsLO = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// button margins
paramsLO.setMargins(0, 0, 0, 0);
// button height/width *pixels*
paramsLO.height = 75;
paramsLO.width = 75;
btnLO.setOrientation(LinearLayout.VERTICAL);
btnLO.setBackgroundColor(5); // not working correctly
//buttons
Button b1 = new Button(this);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// intent.setType("image/*");
// startActivityForResult(intent, 0);
startDialog();
}
});
// int i1Btn = 0;
// ImageButton i1 = new ImageButton(this);
// i1Btn = R.drawable.ic_launcher;
/// i1.setImageResource(i1Btn);
imageView1 = new ImageView(this);
//text for buttons
b1.setText("Arrow");
//displays buttons with parameters (if any)
btnLO.addView(b1, paramsLO);
// btnLO.addView(i1, paramsLO);
btnLO.addView(imageView1, paramsLO);
btnLO.setGravity(Gravity.LEFT | Gravity.CENTER_HORIZONTAL);
this.addContentView(btnLO, new LayoutParams());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (requestCode == GALLERY_PICTURE) {
Uri uri = imageReturnedIntent.getData();
if (uri != null) {
// User had pick an image.
Cursor cursor = getContentResolver().query(uri, new String[]
{ android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
final String imageFilePath = cursor.getString(0);
File photos = new File(imageFilePath);
yourSelectedImage = decodeFile(photos);
yourSelectedImage =
Bitmap.createScaledBitmap(yourSelectedImage, 150, 150, true);
imageView1.setImageBitmap(yourSelectedImage);
cursor.close();
}
else {
Toast toast = Toast.makeText(this, "No Image is selected.",
Toast.LENGTH_LONG);
toast.show();
}
}
else if (requestCode == CAMERA_PICTURE) {
if (imageReturnedIntent.getExtras() != null) {
// here is the image from camera
yourSelectedImage = (Bitmap)
imageReturnedIntent.getExtras().get("data");
imageView1.setImageBitmap(yourSelectedImage);
}
}
}
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 <
REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null,
o2);
}
catch (FileNotFoundException e) {
}
return null;
}
private void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("Upload Pictures Option");
myAlertDialog.setMessage("How do you want to set your picture?");
myAlertDialog.setPositiveButton("Gallery", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pictureActionIntent = new Intent(Intent.ACTION_GET_CONTENT,
null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
}
});
myAlertDialog.setNegativeButton("Camera", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pictureActionIntent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File dir = new File(Environment.getExternalStorageDirectory()
+ "/myFolder23");
if(dir.exists() && dir.isDirectory()) {
// do something here
}
else{
//create dir here
dir.mkdir();
}
Intent pictureActionIntent = new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new
File(SDCARD_ROOT_PATH +
SAVE_PATH_IN_SDCARD,IMAGE_CAPTURE_NAME)));
startActivityForResult(pictureActionIntent,CAMERA_PICTURE);
}
});
myAlertDialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:0)
您可以使用以下方法根据需要捕获多个图像
private class CaptureThread extends Thread {
@Override
public void run() {
int count = 0;
while(count < mNo) {
mFileName = mLocation + "/pic" + count + ".jpg";
mCamera.takePicture(null, mPictureCallback, mPictureCallback);
count++;
try {
Thread.sleep(3000);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
}
}