在android中捕获和存储多个图像

时间:2015-02-24 06:22:49

标签: android

这是图像捕获存储和显示部分的代码。显示部分不工作。因为图像存储在子文件夹中,但是当我显示最新捕获的图像时,它不能这样做。请帮忙

package org.example.beproj;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class WashroomData extends Activity {
EditText editText2;
private boolean success=false;
Button button1;

Button enter;
ListView listView1;
List<String> li;
private String temp;
private static final int CAMERA_REQUEST = 1888;
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageButton imageButton1;
ImageView imageView1;
int test=0;
static String path;
static File w,m;
private File imagFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wash);
getActionBar().setDisplayHomeAsUpEnabled(true);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
TextView t=(TextView) findViewById(R.id.editText2);
final String label=t.getText().toString();
w=new File(Environment.getExternalStorageDirectory(),"Washroom");
m=new File(w,label.trim());
path=m.getAbsolutePath();
if (!m.exists()) {

            success = true;
            success = m.mkdirs();


        }
        if (success) {

            Toast.makeText(WashroomData.this,"Successfully    created"+label, Toast.LENGTH_LONG).show();
        //  Intent i =new Intent(WashroomData.this,Enter_washroom_data.class);
        //startActivity(i);

        setContentView(R.layout.enter_data);
        enter=(Button)findViewById(R.id.enter);

        enter.setOnClickListener(new View.OnClickListener() {       
        @Override
        public void onClick(View v)
        {


            EditText editText1=(EditText)findViewById(R.id.editText1);
            li=new ArrayList<String>();
             temp=editText1.getText().toString();
            Toast.makeText(WashroomData.this, temp, Toast.LENGTH_LONG).show();
            li.add(temp);
            listView1=(ListView)findViewById(R.id.listView1);   
            ArrayAdapter<String> adp=new ArrayAdapter<String> (WashroomData.this,android.R.layout.simple_list_item_1,li);
            listView1.setAdapter(adp);
        }
        });
        imageButton1=(ImageButton)findViewById(R.id.imageButton1);
        imageButton1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String xyz="Image_"+String.valueOf(test) +".jpg";


                imagFile = new File(path,xyz);

                while(imagFile.exists())
                {
                    test++;
                    xyz="Image_"+String.valueOf(test) +".jpg";
                    imagFile = new File(path,xyz);

                }
                Intent localIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                localIntent.putExtra("output", Uri.fromFile(imagFile));
                startActivityForResult(localIntent, 1888);
                Toast.makeText(WashroomData.this, "okay", Toast.LENGTH_LONG).show();
            }
        });


        } else {
            Toast.makeText(WashroomData.this,"Failed to create", Toast.LENGTH_LONG).show(); 
        }


    }
});

}
protected void onActivityResult(int paramInt1, int paramInt2, Intent       paramIntent)
{
  super.onActivityResult(paramInt1, paramInt2, paramIntent);
    imageView1=(ImageView)findViewById(R.id.imageView1);
  Bitmap localBitmap = (Bitmap)paramIntent.getExtras().get("data");
  imageView1.setImageBitmap(localBitmap);


if (imagFile.exists()) {
  Toast.makeText(this, "Image Captured" , Toast.LENGTH_SHORT).show();
} else {
  Toast.makeText(this, "Image not stored", Toast.LENGTH_SHORT).show();
}
return;
}


public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==android.R.id.home)
{
    finish();
}
return super.onOptionsItemSelected(item);

}

1 个答案:

答案 0 :(得分:1)

  // bimatp factory
            BitmapFactory.Options options = new BitmapFactory.Options();

            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                    options);
            imageView1.setImageBitmap(bitmap);