如何从SD卡拍摄图像

时间:2015-08-10 03:46:38

标签: android listview android-intent

我有一个Editext和一个Button(在Activity1中),当用户在Edittext中写入数字(n)并单击按钮=> show(n)ImageView ==>我可以做到  但是我无法在Activity2中从SD卡中获取图像。我该怎么办。任何人都可以帮助我?

Mainclass

    public class MainActivity extends Activity implements AdapterView.OnItemLongClickListener {
    static Integer[] INTENT_REQUEST_CODE_BROWSE_PICTURE;
    ListView list;
    String textEncode = null;
    mylist my;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.list);
        Intent intent = getIntent();
        int b = intent.getIntExtra("number", 0);
        getdata(b);
        list.setOnItemLongClickListener(this);
    }
    public void getdata(int a) {
          SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
         textEncode = shre.getString("image","");
        String[] itemname = new String[a];
        Integer[] imgid = new Integer[a];
        for (int i = 0; i < a; i++) {
            itemname[i] = "DEMO";
              byte[] c = Base64.decode(textEncode, Base64.DEFAULT);
             Bitmap bitmap = BitmapFactory.decodeByteArray(c, 0, c.length);
            imgid[i] = R.drawable.sample_2;
            mylist adapter = new mylist(this, itemname, imgid);
            list.setAdapter(adapter);
            my.imageView.setImageBitmap(bitmap);
            //  list.setImageBitmap(bitmap);
        }
    }
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent intent) {
        Intent intent1 = getIntent();
        int b = intent1.getIntExtra("number", 0);
        for (int i = 0; i < b; i++) {
            INTENT_REQUEST_CODE_BROWSE_PICTURE = new Integer[b];
            if (INTENT_REQUEST_CODE_BROWSE_PICTURE[i] == i) {
                if (resultCode == Activity.RESULT_OK) {
                    execute(intent, "image", b);
                }
            }
        }
    }
    public void execute(Intent intent, String a, int d) {
        InputStream stream;
        try {
            stream = getContentResolver().openInputStream(intent.getData());
            Bitmap realImage = BitmapFactory.decodeStream(stream);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            realImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();
            String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
            SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor edit = shre.edit();
            for (int i = 0; i < d; i++) {
                edit.putString(a + String.valueOf(i), encodedImage);
                edit.commit();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }``
     @Override
   public boolean onItemLongClick(AdapterView<?> parent, View view, int 
position, long id) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivity(intent);
       Intent intent1=getIntent();
       int b=intent1.getIntExtra("number", 0);
      for (int i = 0; i <b; i++) {
         if(position==i){
               startActivityForResult(intent,
                        INTENT_REQUEST_CODE_BROWSE_PICTURE[i]);
           }
  }
     return true;

   }
}

MYLIST(活动)

public class mylist extends ArrayAdapter<String> {

    private  Activity context;
    private  String[] itemname;
    private  Integer[] imgid;
    ImageView imageView;
    public mylist(Activity context, String[] itemname, Integer[] imgid) {
        super(context, R.layout.activity_mylist2, itemname);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.itemname = itemname;
        this.imgid = imgid;
    }

    public View getView(int position,View view,ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.activity_mylist2, null, true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
         imageView = (ImageView) rowView.findViewById(R.id.icon);
        TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);

        txtTitle.setText(itemname[position]);
        imageView.setImageResource(imgid[position]);
        extratxt.setText("Description "+itemname[position]);
        return rowView;
    };


}

Sendata(活动)

public class Senddata extends ActionBarActivity {

    Button button;
    EditText edit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_senddata);

        button=(Button) findViewById(R.id.send);
        edit=(EditText) findViewById(R.id.text1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),MainActivity.class);
                intent.putExtra("number",Integer.parseInt(edit.getText().toString()));
                startActivity(intent);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

下面的代码应该是帮助

File f = new File("/mnt/sdcard/photo.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView1.setImageBitmap(bmp);