我正在尝试制作应用来制作照片并将其张贴在布局中。下一个代码可以做到这一点,但如果我有10张照片,我不知道如何识别每个imageview。
package com.selectphoto.dialog;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.selectphoto.R;
import com.selectphoto.ui.SelectPhotoBase;
import java.io.File;
public class TakePictureDialog extends Activity {
private Uri outputFileUri;
private static final int TAKE_PICTURE = 100;
private LinearLayout layout_add_photo;
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.dialog_add_photo);
final Button bt_return = (Button)findViewById(R.id.bt_return);
final Button bt_take_photo = (Button)findViewById(R.id.bt_take_photo);
bt_return.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
bt_take_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openTakePicture();
}
});
}
/**
* Call the camera app to take a picture.
*/
public void openTakePicture() {
// Create an output file.
File file = new File(Environment.getExternalStorageDirectory(), "test.jpg");
outputFileUri = Uri.fromFile(file);
// Generate the Intent.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
// Launch the camera app.
startActivityForResult(intent, TAKE_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
layout_add_photo = (LinearLayout)findViewById(R.id.layout_add_photo);
if (requestCode == TAKE_PICTURE) {
if(data != null){
if(WorldersBase.DEBUG)
Log.e(WorldersBase.TAG, "Error en onActivityResult if");
if(data.hasExtra("data")){
Bitmap thumbnail = data.getParcelableExtra("data");
// Añadir al layout la imagen.
}
} else{
if(WorldersBase.DEBUG)
Log.e(WorldersBase.TAG, "Error en onActivityResult else");
// If there is no thumbnail image data, the image will have been stored in the
// target output URI.
// Resize the full image to fit in out image view.
int width = layout_add_photo.getWidth();
int height = 40;
BitmapFactory.Options factoryOptions = new BitmapFactory.Options();
factoryOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(outputFileUri.getPath(), factoryOptions);
int imageWidth = factoryOptions.outWidth;
int imageHeight = factoryOptions.outHeight;
// Determine how much to scale down the image.
int scaleFactor = Math.min(imageWidth/width, imageHeight/height);
// Decode the image file into a Bitmap sized to fill the View.
factoryOptions.inJustDecodeBounds = false;
factoryOptions.inSampleSize = scaleFactor;
factoryOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(outputFileUri.getPath(), factoryOptions);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
layout_add_photo.addView(imageView);
}
}
}
}
另一个问题是我需要将图片调整到更小的尺寸,我想640x480左右。但是按照Loading Large Bitmaps Efficiently - On developer.android.com的教程,我有点迷失了。我有下一个方法,但我认为它是加载项目的资源,而不是文件(如拍摄的照片),所以我不知道如何实现它。方法是:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
我怎么能这样做?
谢谢。
答案 0 :(得分:1)
imageview.setId(someintvalue);
解决这个好友;