如何在imageview中显示超过1个捕获的图像

时间:2015-06-02 10:52:42

标签: android

我正在从相机拍摄3张图像但是当我尝试在图像视图中显示它时,首先屏幕进入横向模式然后它消失并且图像视图变为空白。我甚至做过“screenOrientation:portrait”仍然只在三星galaxy tab3 lite设备上出现问题。这是我的代码:

public class xxxxxx2 extends Activity{



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_lodge_complaints2);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);}


    mHelper=new DatabaseHelper(this);
    imgVw1=(ImageView)findViewById(R.id.imgComp1);
    imgVw2=(ImageView)findViewById(R.id.imgComp2);
    imgVw3=(ImageView)findViewById(R.id.imgComp3);

    SharedPreferences prefReq=PreferenceManager.getDefaultSharedPreferences(xxx2.this);
    String reqcode2=prefReq.getString("REQ_CODE", "");

    if(thumb2==null && reqcode2.equals("1"))
    {
        thumb2=retreivePic1();
    }

    if(thumb3==null && reqcode2.equals("2"))
    {
        thumb3=retreivePic2();
    }

    if(thumb4==null && reqcode2.equals("3"))
    {
        thumb4=retreivePic3();
    }

     appLocprov=new ApplicationLocationProvider(LodgeComplaints2.this);

    //btnImage=(Button)findViewById(R.id.btnClick);
    btnSend=(Button)findViewById(R.id.btnSend);

    btncap1=(Button)findViewById(R.id.btnCap1);
    btncap2=(Button)findViewById(R.id.btnCap2);
    btncap3=(Button)findViewById(R.id.btnCap3);

    btncap1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SharedPreferences prefReq1=PreferenceManager.getDefaultSharedPreferences(xxxxxx2.this);
            SharedPreferences.Editor editor =prefReq1.edit();
            editor.putString("REQ_CODE", "1");
            editor.apply();
            final CharSequence[] items={"Choose from Gallery","Capture an Image"};
            AlertDialog.Builder builder=new AlertDialog.Builder(LodgeComplaints2.this);
            builder.setTitle("Make your Selection");
            builder.setItems(items, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    if(items[which].equals("Choose from Gallery"))
                    {
                        Intent inGal=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(inGal,RESULT_LOAD_IMAGE);
                    }

                    else if(items[which].equals("Capture an Image"))
                    {
                        Intent inCap=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        //final String Comp_Name2=txtComplCat.getText().toString();
                         //final String Comp_Name2=Comp_Name;

                        // inCap.putExtra("Complaint", val2);
                        startActivityForResult(inCap, CAMERA_PIC_REQUEST);
                    }
                }
            });

            AlertDialog alert=builder.create();
            alert.show();
        }
    });

    btncap2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            SharedPreferences prefReq1=PreferenceManager.getDefaultSharedPreferences(LodgeComplaints2.this);
            SharedPreferences.Editor editor =prefReq1.edit();
            editor.putString("REQ_CODE", "2");
            editor.apply();
            final CharSequence[] items={"Choose from Gallery","Capture an Image"};
            AlertDialog.Builder builder=new AlertDialog.Builder(LodgeComplaints2.this);
            builder.setTitle("Make your Selection");
            builder.setItems(items, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    if(items[which].equals("Choose from Gallery"))
                    {
                        Intent inGal=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(inGal,RESULT_LOAD_IMAGE);
                    }

                    else if(items[which].equals("Capture an Image"))
                    {
                        Intent inCap=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        //final String Comp_Name2=txtComplCat.getText().toString();
                         //final String Comp_Name2=Comp_Name;

                        // inCap.putExtra("Complaint", val2);
                        startActivityForResult(inCap, CAMERA_PIC_REQUEST);
                    }
                }
            });

            AlertDialog alert=builder.create();
            alert.show();
        }
    });

    btncap3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPreferences prefReq1=PreferenceManager.getDefaultSharedPreferences(xxxxxx2.this);
            SharedPreferences.Editor editor =prefReq1.edit();
            editor.putString("REQ_CODE", "3");
            editor.apply();
            // TODO Auto-generated method stub
            final CharSequence[] items={"Choose from Gallery","Capture an Image"};
            AlertDialog.Builder builder=new AlertDialog.Builder(LodgeComplaints2.this);
            builder.setTitle("Make your Selection");
            builder.setItems(items, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    if(items[which].equals("Choose from Gallery"))
                    {
                        Intent inGal=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(inGal,RESULT_LOAD_IMAGE);
                    }

                    else if(items[which].equals("Capture an Image"))
                    {
                        Intent inCap=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        //final String Comp_Name2=txtComplCat.getText().toString();
                         //final String Comp_Name2=Comp_Name;

                        // inCap.putExtra("Complaint", val2);
                        startActivityForResult(inCap, CAMERA_PIC_REQUEST);
                    }
                }
            });

            AlertDialog alert=builder.create();
            alert.show();
        }
    });



    //deleteFiles("image1.jpg");
    //deleteFiles("image2.jpg");
    //deleteFiles("image3.jpg");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.lodge_complaints2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if(requestCode==CAMERA_PIC_REQUEST && resultCode==RESULT_OK && data!=null)
    {

    Bitmap thumb=(Bitmap)data.getExtras().get("data");
    //img.setImageBitmap(thumb);

    ByteArrayOutputStream bytes=new ByteArrayOutputStream();
    thumb.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        /**if(countImage==1)
        {
        imgVw1.setImageBitmap(thumb);
        }

        else if(countImage==2)
        {
        imgVw2.setImageBitmap(thumb);
        }

        else if(countImage==3)
        {
        imgVw3.setImageBitmap(thumb);
        } 

       countImage++; **/

       SharedPreferences prefReq=PreferenceManager.getDefaultSharedPreferences(LodgeComplaints2.this);
        String reqcode=prefReq.getString("REQ_CODE", "");

        if(reqcode.equals("1"))
        {
            thumb2=thumb;
            ByteArrayOutputStream bos1=new ByteArrayOutputStream();
            thumb2.compress(CompressFormat.JPEG, 100, bos1);
            imgByte1=bos1.toByteArray();
            File file=new File(Environment.getExternalStorageDirectory()+File.separator+"image1.jpg");
            try{
                file.createNewFile();
                FileOutputStream fo=new FileOutputStream(file);
                fo.write(bytes.toByteArray());
                fo.close();
            }

            catch(Exception exc)
            {
                exc.printStackTrace();
            }
            //imgVw1.setImageBitmap(retreivePic1());
        }
        else if(reqcode.equals("2"))
        {
            thumb3=thumb;
            ByteArrayOutputStream bos2=new ByteArrayOutputStream();
            thumb3.compress(CompressFormat.JPEG, 100, bos2);
            imgByte2=bos2.toByteArray();
            File file=new File(Environment.getExternalStorageDirectory()+File.separator+"image2.jpg");
            try{
                file.createNewFile();
                FileOutputStream fo=new FileOutputStream(file);
                fo.write(bytes.toByteArray());
                fo.close();
            }

            catch(Exception exc)
            {
                exc.printStackTrace();
            }
            //imgVw2.setImageBitmap(retreivePic2());
        }

        else if(reqcode.equals("3"))
        {
            thumb4=thumb;
            ByteArrayOutputStream bos3=new ByteArrayOutputStream();
            thumb4.compress(CompressFormat.JPEG, 100, bos3);
            imgByte3=bos3.toByteArray();
            File file=new File(Environment.getExternalStorageDirectory()+File.separator+"image3.jpg");
            try{
                file.createNewFile();
                FileOutputStream fo=new FileOutputStream(file);
                fo.write(bytes.toByteArray());
                fo.close();
            }

            catch(Exception exc)
            {
                exc.printStackTrace();
            }
            //imgVw3.setImageBitmap(retreivePic3());
        }

    //imgVw1.setImageBitmap(thumb);

/** File file=new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpg");
    try{
        file.createNewFile();
        FileOutputStream fo=new FileOutputStream(file);/..
        fo.write(bytes.toByteArray());
        fo.close();
    }

    catch(Exception exc)
    {
        exc.printStackTrace();
    } **/

    }

    else if(requestCode==RESULT_LOAD_IMAGE && resultCode==RESULT_OK && null!=data)
    {
        Uri selectedImage=data.getData();
        String [] filepathColumn={MediaStore.Images.Media.DATA};
        Cursor cursor=getContentResolver().query(selectedImage, filepathColumn, null, null, null);
        cursor.moveToLast();
        int columnIndex=cursor.getColumnIndex(filepathColumn[0]);
        String picturePath=cursor.getString(columnIndex);
        cursor.close();
        thumbnail=(BitmapFactory.decodeFile(picturePath));

        /**if(countImage==1){
            imgVw1.setImageBitmap(thumbnail);
            }

            else if(countImage==2)
            {
            imgVw2.setImageBitmap(thumbnail);
            }

            else if(countImage==3)
            {
            imgVw3.setImageBitmap(thumbnail);
            } 

            countImage++; **/
        SharedPreferences prefReq=PreferenceManager.getDefaultSharedPreferences(LodgeComplaints2.this);
        String reqcode=prefReq.getString("REQ_CODE", "");

        if(reqcode.equals("1"))
        {
            thumb2=thumbnail;
            ByteArrayOutputStream bos1=new ByteArrayOutputStream();
            thumb2.compress(CompressFormat.JPEG, 100, bos1);
            imgByte1=bos1.toByteArray();
            //imgVw1.setImageBitmap(thumb2);
        }
        else if(reqcode.equals("2"))
        {
            thumb3=thumbnail;
            ByteArrayOutputStream bos2=new ByteArrayOutputStream();
            thumb3.compress(CompressFormat.JPEG, 100, bos2);
            imgByte2=bos2.toByteArray();
            //imgVw2.setImageBitmap(thumb3);
        }

        else if(reqcode.equals("3"))
        {
            thumb4=thumbnail;
            ByteArrayOutputStream bos3=new ByteArrayOutputStream();
            thumb4.compress(CompressFormat.JPEG, 100, bos3);
            imgByte3=bos3.toByteArray();
            //imgVw3.setImageBitmap(thumb4);
        }

    }


    }

}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

public Bitmap retreivePic()
{
    File f=new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpg");
    Bitmap bmp=BitmapFactory.decodeFile(f.getAbsolutePath());
    return bmp;
}

public Bitmap retreivePic1()
{
    File f=new File(Environment.getExternalStorageDirectory()+File.separator+"image1.jpg");
    Bitmap bmp=BitmapFactory.decodeFile(f.getAbsolutePath());
    return bmp;
}

public Bitmap retreivePic2()
{
    File f=new File(Environment.getExternalStorageDirectory()+File.separator+"image2.jpg");
    Bitmap bmp=BitmapFactory.decodeFile(f.getAbsolutePath());
    return bmp;
}

public Bitmap retreivePic3()
{
    File f=new File(Environment.getExternalStorageDirectory()+File.separator+"image3.jpg");
    Bitmap bmp=BitmapFactory.decodeFile(f.getAbsolutePath());
    return bmp;
}



 }

0 个答案:

没有答案