2014-11-14 5 views
2

내 갤러리에서 그림을 선택하고 ImageView에 표시하려고합니다. 사진이 표시되지 않습니다. 왜? 이것은 수명주기의 문제입니까? 또는 setImageBitmap()을 사용하는 방법이 잘못 되었습니까? 어떻게 수정합니까? 도와주세요.갤러리의 내 그림이 표시되지 않는 이유는 무엇입니까?

--This는

public class RegisterView extends Activity { 
TextView textCategory; 
ImageView imageButton; 
EditText productName,price; 
private final static int CATEGORY=0; 
private final static int GALLERY=1; 
private final static int CAMERA=2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.register_view); 
    textCategory=(TextView)findViewById(R.id.text_category); 
    imageButton=(ImageView)findViewById(R.id.select_image); 
    productName=(EditText)findViewById(R.id.product_name); 
    price=(EditText)findViewById(R.id.price); 
    Button categoryButton=(Button)findViewById(R.id.category_button); 
    categoryButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent=new Intent(RegisterView.this,CategoryList.class); 
      startActivityForResult(intent,CATEGORY); 
     } 
    }); 
    ImageView selectImage=(ImageView)findViewById(R.id.select_image); 
    selectImage.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      DialogFragment dialog=new SelectImageDialog(); 
      dialog.show(getFragmentManager(),"dialog"); 
     } 
    }); 
    Button saveButton=(Button)findViewById(R.id.save_button); 
    saveButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if(textCategory.getText().toString().equals("") || imageButton.getDrawable()==null || productName.getText().toString().equals("") || price.getText().toString().equals("")) { 
       Bundle bundle=new Bundle(); 
       bundle.putString("message","全ての項目を入力してください"); 
       DialogFragment dialog=new CategoryList.AttensionDialog(); 
       dialog.setArguments(bundle); 
       dialog.show(getFragmentManager(),"dialog"); 
      }else{ 
       DialogFragment dialog = new SaveTimeDialog(); 
       dialog.show(getFragmentManager(), "dialog"); 
      } 
     } 
    }); 
} 

public static class SelectImageDialog extends DialogFragment{ 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState){ 
     AlertDialog.Builder builder=new AlertDialog.Builder(getActivity()); 
     builder.setMessage("画像の設定方法を選択してください").setPositiveButton("カメラ",new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       Intent intent=new Intent(); 
       intent.setAction("android.media.action.IMAGE_CAPTURE"); 
       startActivityForResult(intent,CAMERA); 
      } 
     }).setNegativeButton("ギャラリー",new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       Intent intent=new Intent(); 
       intent.setAction(intent.ACTION_GET_CONTENT); 
       intent.setType("image/*"); 
       startActivityForResult(intent,GALLERY); 
      } 
     }); 
     return builder.create(); 
    } 
} 

public static class SaveTimeDialog extends DialogFragment{ 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState){ 
     AlertDialog.Builder builder=new AlertDialog.Builder(getActivity()); 
     builder.setMessage("データを登録しますか?").setPositiveButton("はい",new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       //入力されたデータをデータベースに登録する 
      } 
     }).setNegativeButton("いいえ",new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 

      } 
     }); 
     return builder.create(); 
    } 
} 

@Override 
public void onActivityResult(int requestCode,int resultCode,Intent data){ 
    if(requestCode==CATEGORY){ 
     if(resultCode==RESULT_OK) { 
      Bundle bundle = data.getExtras(); 
      textCategory.setText(bundle.getString("item")); 
     } 
    }else if(requestCode==GALLERY){ 
     if(resultCode==RESULT_OK){ 
      try{ 
       InputStream is=getContentResolver().openInputStream(data.getData()); 
       Bitmap bitmap=BitmapFactory.decodeStream(is); 
       is.close(); 
       imageButton.setImageBitmap(bitmap); 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
     } 
    }else if(requestCode==CAMERA){ 
     if(resultCode==RESULT_OK){ 
      Bitmap bitmap=(Bitmap)data.getExtras().get("data"); 
      imageButton.setImageBitmap(savePic(bitmap)); 
     } 
    } 
} 

public Bitmap savePic(Bitmap bitmap){ 
    String path= Environment.getExternalStorageDirectory().getPath(); 
    try { 
     FileOutputStream outputStream = new FileOutputStream(path+"pic.png"); 
     bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream); 
     outputStream.close(); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
    String[] paths = {Environment.getExternalStorageDirectory().getPath()+"/pic.png"}; 
    String[] mimeType = {"image/png"}; 
    MediaScannerConnection.scanFile(getApplicationContext(), paths, mimeType, new MediaScannerConnection.OnScanCompletedListener() { 
     @Override 
     public void onScanCompleted(String s, Uri uri) { 
     } 
    }); 
    return bitmap; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.my, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

그 수업 가득하고이 로그 캣의 메시지입니다.

11-14 15:31:23.126 18201-18201/com.example.appdev.shop D/libEGL﹕ loaded /system/lib/egl/libEGL_adreno200.so 
11-14 15:31:23.126 18201-18201/com.example.appdev.shop D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
11-14 15:31:23.126 18201-18201/com.example.appdev.shop D/libEGL﹕ loaded /system/lib/egl/libGLESv2_adreno200.so 
11-14 15:31:23.126 18201-18201/com.example.appdev.shop I/Adreno200-EGL﹕ <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.5.6.04.02.02.093.081_msm8960_JB_2.5.6_CL3544079_release_AU (CL3544079) 
    Build Date: 06/27/13 Thu 
    Local Branch: 
    Remote Branch: m/jb_2.5.6 
    Local Patches: NONE 
    Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.6.04.02.02.093.081 + NOTHING 
11-14 15:31:23.156 18201-18201/com.example.appdev.shop D/OpenGLRenderer﹕ Enabling debug mode 0 
11-14 15:31:27.301 18201-18201/com.example.appdev.shop W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection 
+0

당신이 당신의 전체 클래스 코드를 넣을 수 있습니다 – koutuk

+0

definatly 문제가 해결됩니다 안드로이드에 "어떻게 이미지를 캡처"구글에 입력하세요? –

+0

사용중인 OS 버전을 지정할 수 있습니까? galary에서 이미지를 선택하면 kitkat에 문제가 생기기 때문에 –

답변

0

KITKAT의 문제는 해결할 필요가 있습니다.

public static final String KITKAT_VALUE = 1002; 

    Intent intent; 

    if (Build.VERSION.SDK_INT < 19){ 
     intent = new Intent(); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     intent.setType("*/*"); 
     startActivityForResult(intent, KITKAT_VALUE); 
    } 
    else 
    { 
     intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 
     intent.addCategory(Intent.CATEGORY_OPENABLE); 
     intent.setType("*/*"); 
     startActivityForResult(intent, KITKAT_VALUE); 
    } 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode == Activity.RESULT_OK && data!=null) { 
      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(); 
      imageButton.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

    } 
} 

참조 : Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT

+0

이 코드를 시도했지만 작동하지 않습니다. ... 죄송합니다 – tarofess

+0

출력은 무엇입니까 ?? 오류가 있습니까? –

+0

오류가 표시되지 않습니다. 갤러리에서 그림을 선택할 수는 있지만 그림은 ImageView에 표시되지 않습니다. – tarofess

관련 문제