0

写真を作成してレイアウトに投稿するアプリを作成しようとしています。次のコードはこれを作成しますが、10 枚の写真を取得した場合に各イメージビューを識別する方法がわかりません。

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);
            }
        }
    }
}

もう 1 つの質問は、写真のサイズを小さいサイズに変更する必要があるということです。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);
}

どうすればそれができますか?

ありがとうございました。

4

1 に答える 1

1
imageview.setId(someintvalue);

この相棒を解決します。

于 2013-11-05T09:00:31.330 に答える