如何区分图像是否是从Android的画廊或照相机来了吗?来了、是从、照相机、画廊

2023-09-12 10:40:20 作者:隔壁老王

我在做一个应用程序,我可以选择一个图片从画廊或我可以把图像从相机和我移动到下一个活动所选择的image.There我要分辨是否图像从画廊或相机来了。我是没有得到任何知道该怎么做吧。我感到我提供code.手电筒帮助我如何做到这一点。

我的第一个活动是

 进口java.io.ByteArrayOutputStream中;
进口android.app.Activity;
进口android.content.Intent;
进口android.database.Cursor;
进口android.graphics.Bitmap;
进口android.graphics.Color;
进口android.net.Uri;
进口android.os.Bundle;
进口android.provider.MediaStore;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ImageButton;
进口android.widget.ImageView;
进口android.widget.Toast;



  公共类LauncherActivity扩展活动
   {
 私有静态最终诠释CAMERA_REQUEST = 1888;
 私有静态诠释RESULT_LOAD_IMAGE = 1;
 ImageButton的摄像头;
 的ImageButton画廊;
 ImageView的gallery_image;

 保护无效的onCreate(包paramBundle)
 {
 super.onCreate(paramBundle);
 的setContentView(R.layout.launcher);
 画廊=(的ImageButton)findViewById(R.id.select_photo);
 摄像头=(的ImageButton)findViewById(R.id.take_photo);





   gallery.setOnClickListener(新OnClickListener(){

    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
            意图gallery_intent =新的意图(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(gallery_intent,RESULT_LOAD_IMAGE);
     }
    });

     camera.setOnClickListener(新OnClickListener(){

    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根
                    意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
                    startActivityForResult(cameraIntent,CAMERA_REQUEST);
    }
});

  }
   保护无效onActivityResult(INT申请code,INT结果code,意图数据){
  super.onActivityResult(要求code,因此code,数据);



  如果(要求code == RESULT_LOAD_IMAGE和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!NULL =数据){



      乌里selectedImage = data.getData();

      的String [] filePathColumn = {MediaStore.Images.Media.DATA};



      光标光标= getContentResolver()查询(selectedImage,
              filePathColumn,NULL,NULL,NULL);
      cursor.moveToFirst();

      INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
      字符串picturePath = cursor.getString(参数:columnIndex);
      cursor.close();
      意向意图=新的意图(LauncherActivity.this,MainActivity.class);
      intent.putExtra(路径,picturePath);
      startActivity(意向);

    如果(要求code == CAMERA_REQUEST和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!数据= NULL){

      。位图photo_from_camera =(位图)data.getExtras()获得(数据);

      ByteArrayOutputStream流=新ByteArrayOutputStream();
      photo_from_camera.com preSS(Bitmap.Com pressFormat.PNG,100,流);
      字节[]的字节数组= stream.toByteArray();
      意向意图=新的意图(LauncherActivity.this,MainActivity.class);
      intent.putExtra(形象,字节);
      startActivity(意向);


    }
  }
  }
 

和我的第二个活动是

 公共类MainActivity扩展活动
   {
   位图缩放;
 保护无效的onCreate(包paramBundle)
  {
  super.onCreate(paramBundle);
  的setContentView(R.layout.activity_main);


   如果(//条件将图像从画廊的未来)
   {
   捆绑额外= getIntent()getExtras()。
   字符串picturePath = extras.getString(路径);
  缩放= BitmapFactory.de codeFILE(picturePath);
  //的事情我都与位图,我知道
   }
  如果(//条件将图像从相机未来)
  {
    捆绑额外= getIntent()getExtras()。
   字节[]的字节数组= extras.getByteArray(图像);
   点阵位图= BitmapFactory.de codeByteArray(字节数组,0,byteArray.length);
  缩放= Bitmap.createScaledBitmap(位图,宽度,高度,真);
  //的事情我都与位图,我知道
  }
  }
   }
 

在这里,我没有得到code写的,如果statement.please帮助me.Thanks条件提前。

解决方案

  startActivityForResult(cameraIntent,CAMERA_REQUEST);
startActivityForResult(gallery_intent,RESULT_LOAD_IMAGE);
 
你的手机到底安不安全 看看Android和iOS是如何防御恶意应用的吧

对于你的要求code可以区分图像从画廊或相机

未来

 布尔库;

     如果(要求code == CAMERA_REQUEST){
        //然后从摄像机图像

画廊= TRUE;
    galleryIntent意图=新的意图(LauncherActivity.this,MainActivity.class);
          intent.putExtra(路径,picturePath);
         intent.putExtra(isfrom,画廊);
    startActivity(意向);
        }
        如果(要求code == RESULT_LOAD_IMAGE){
    galleryIntent意图=新的意图(LauncherActivity.this,MainActivity.class);
          intent.putExtra(路径,picturePath);
         intent.putExtra(isfrom,画廊);
    startActivity(意向);
        //然后从展厅形象
        }
 

在第二个活动就可以得到布尔从intent.if是从画廊的真实形象

I am making an app in which i can pick an image from gallery or i can take image from camera and i am moving to next activity with the selected image.There I want to distinguish whether the image is coming from gallery or camera.I am not getting any idea how to do it.I am providing my code.Please help me how to do this.

My first activity is

import java.io.ByteArrayOutputStream;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;



  public class LauncherActivity extends Activity 
   {
 private static final int CAMERA_REQUEST = 1888; 
 private static int RESULT_LOAD_IMAGE = 1;
 ImageButton camera;
 ImageButton gallery;
 ImageView gallery_image;

 protected void onCreate(Bundle paramBundle)
 {
 super.onCreate(paramBundle);
 setContentView(R.layout.launcher);
 gallery = (ImageButton)findViewById(R.id.select_photo);
 camera = (ImageButton)findViewById(R.id.take_photo);





   gallery.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub        
            Intent gallery_intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(gallery_intent, RESULT_LOAD_IMAGE);
     }
    });

     camera.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub        
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
    }
});

  }
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);



  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.moveToFirst();

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      String picturePath = cursor.getString(columnIndex);
      cursor.close();
      Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
      intent.putExtra("path", picturePath);
      startActivity(intent); 

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data!=null) {  

      Bitmap photo_from_camera = (Bitmap) data.getExtras().get("data"); 

      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      photo_from_camera.compress(Bitmap.CompressFormat.PNG, 100, stream);
      byte[] byteArray = stream.toByteArray();
      Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
      intent.putExtra("image", byteArray);
      startActivity(intent);


    }  
  }
  }

and my second activity is

   public class MainActivity extends Activity 
   {
   Bitmap scaled;
 protected void onCreate(Bundle paramBundle)
  {
  super.onCreate(paramBundle);
  setContentView(R.layout.activity_main);


   if(//condition to The image coming from gallery)
   {
   Bundle extras=getIntent().getExtras();
   String picturePath=extras.getString("path");
  scaled=BitmapFactory.decodeFile(picturePath);
  //the things i have to do with bitmap that i know
   }    
  if(//Condition to the image coming from camera)
  {
    Bundle extras = getIntent().getExtras();
   byte[] byteArray = extras.getByteArray("image");
   Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
  scaled = Bitmap.createScaledBitmap(bitmap, width, height, true);
  //the things i have to do with bitmap that i know
  }
  }
   }

Here I am not getting the code to write the conditions in the if statement.please help me.Thanks in advance.

解决方案

startActivityForResult(cameraIntent, CAMERA_REQUEST); 
startActivityForResult(gallery_intent, RESULT_LOAD_IMAGE);

regarding to your request code you can differentiate image is coming from gallery or camera

 boolean gallery;

     if(requestCode == CAMERA_REQUEST){
        //then image from camera

gallery=true;
    galleryIntent intent = new Intent(LauncherActivity.this, MainActivity.class);
          intent.putExtra("path", picturePath);  
         intent.putExtra("isfrom", gallery); 
    startActivity(intent);
        }
        if(requestCode == RESULT_LOAD_IMAGE){
    galleryIntent intent = new Intent(LauncherActivity.this, MainActivity.class);
          intent.putExtra("path", picturePath);  
         intent.putExtra("isfrom", gallery); 
    startActivity(intent);
        //then image from gallery
        }

in second activity you can get boolean from intent.if it is true image from gallery