2016-09-22 7 views
0

사용자가 이미지를 선택하고 imageview을 다른 activity으로 설정하는 데 사용하도록 허용하고 있습니다. 그러나 imageview은 비어 있습니다. 다음은 내 코드입니다.ImageView는 비어 있습니다. Android

private static final int REQUEST_CODE_GALLERY = 1; 
    private static final int REQUEST_IMAGE_CAPTURE = 2; 

    public ImageView CameraButton; 
    public ImageView GalleryButton; 
    public ImageView example; 
    public Bitmap imageBitmap; 
    public Bitmap bmp; 

    ongallery object = new ongallery(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    GalleryButton = (ImageView) findViewById(R.id.GalleryButton); 
    CameraButton = (ImageView) findViewById(R.id.CameraButton); 
    example = (ImageView) findViewById(R.id.example); 
    GalleryButton.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
           startActivityForResult(gallery, REQUEST_CODE_GALLERY); 
          } 
         }); 
     CameraButton.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
           if (camera.resolveActivity(getPackageManager()) != null) { 
            startActivityForResult(camera, REQUEST_IMAGE_CAPTURE); 
           } 
          } 
         }); 
        } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
    if (requestCode == REQUEST_CODE_GALLERY && resultCode == RESULT_OK && null != data) { 

    Uri chosen = data.getData(); 
    String[] filepath = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = getContentResolver().query(chosen, filepath, null, null, null); 
    cursor.moveToFirst(); 
    int columnIndex = cursor.getColumnIndex(filepath[0]); 
    String photoadd = cursor.getString(columnIndex); 
    cursor.close(); 
    bmp = BitmapFactory.decodeFile(photoadd); 

    try { 
    //Write file 
    String filename = "bitmap.png"; 
    FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE); 
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
    //Cleanup 
    stream.close(); 
    bmp.recycle(); 
    //Pop intent 
    Intent in1 = new Intent(this, ongallery.class); 
    in1.putExtra("picture", filename); 
    startActivity(in1); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    } 
    else if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && null!=data) { 
    Bitmap photo = (Bitmap) data.getExtras().get("data"); 
    try { 
    //Write file 
    String filename = "bitmap.png"; 
    FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE); 
    photo.compress(Bitmap.CompressFormat.PNG, 100, stream); 
    //Cleanup 
    stream.close(); 
    photo.recycle(); 
    //Pop intent 
    Intent in1 = new Intent(this, ongallery.class); 
    in1.putExtra("picture", filename); 
    startActivity(in1); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    } 
    }catch(Exception e){ 
    Toast.makeText(this,"Something went wrong",Toast.LENGTH_SHORT).show(); 
    } 
    } 
    } 


//ongallery.java 
    public class ongallery extends Activity { 
    public ImageView imgView; 
    int xDim; 
    int yDim; 
    String filename; 
    public Bitmap finale = null ; 
    public Bitmap bmp = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.ongallery); 
    imgView = (ImageView) findViewById(R.id.imgView); 
    filename = getIntent().getStringExtra("picture"); 
    try { 
    FileInputStream is = this.openFileInput(filename); 
    bmp = BitmapFactory.decodeStream(is); 
    is.close(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    imgView.setImageBitmap(decoder(filename,400,400)); 
    } 
    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    xDim=imgView.getWidth(); 
    yDim=imgView.getHeight(); 
    } 
    public Bitmap decoder(String filename, int reqWidth, int reqHeight) { 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filename, options); 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 
    options.inJustDecodeBounds = false; 
    finale = BitmapFactory.decodeFile(filename, options); 
    return finale; 
    } 

    int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    int inSampleSize = 1; 
    if (options.outHeight > reqHeight || options.outWidth > reqWidth) { 
    final int halfHeight = options.outHeight/2; 
    final int halfWidth = options.outWidth/2; 
    while ((halfHeight/inSampleSize) > reqHeight && (halfWidth/inSampleSize) >reqWidth) { 
    inSampleSize *= 2; 
    } 
    } 
    return inSampleSize; 
    } 
    } 

답변

0

BitmapFactory.decodeFile()에는 첫 번째 인수로 전체 경로 이름이 필요합니다.

public Bitmap decoder(String filename, int reqWidth, int reqHeight) { 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    String filepath = getFileStreamPath(filename).getPath(); 
    BitmapFactory.decodeFile(filepath, options); 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 
    options.inJustDecodeBounds = false; 
    finale = BitmapFactory.decodeFile(filepath, options); 
    return finale; 
} 
+0

안녕하세요, 감사합니다. 정말 저에게 도움이되었습니다. 그러나, 여전히 카메라 인 텐트를 통해 선택한 이미지는 ImageView에 나타날 때 성능이 저하됩니다. 그것에 대한 제안이 있습니까? –

+0

ImageView 치수를 컨텐츠를 랩핑하도록 조정했으며, 카메라 의도를 사용하여 사진을 찍으면 축소판이 너무 작음을 발견했습니다. –

+0

글쎄, [documentation] (https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE)에 따르면 당신은 Uri를 제공해야하며 풀 사이즈 이미지가 그것에 쓰여질 것입니다. – Iuliia

관련 문제