2012-06-15 5 views
0

저는 애플리케이션에서 작업 중이며 사용자가 갤러리에서 이미지를 선택하여 프레임을 적용 할 수 있기를 원합니다. 모바일 갤러리에 성공적으로 액세스했습니다. 이제는 추가 이미지 처리를 위해 선택한 이미지를 저장하는 방법을 알고 싶습니다. 선택한 이미지가 프레임을 적용하는 데 사용됩니다.이미지를 갤러리에서 선택하고 나중에 사용하기 위해 저장하십시오.

답변

1

여기 있습니다.

Intent intent=new Intent(); 
intent.setType=("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE); 
/*Declare PICK_IMAGE globally : private static final int PICK_IMAGE = 1; */ 

실제 코드이 코드에 여기

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    switch (requestCode) { 
    case PICK_IMAGE: 
     if (resultCode == Activity.RESULT_OK) { 
      Uri selectedImageUri = data.getData(); 
      try { 
       // OI FILE Manager 
       String filemanagerstring = selectedImageUri.getPath(); 

       // MEDIA GALLERY 
       String selectedImagePath = getPath(selectedImageUri); 

       if (selectedImagePath != null) { 
        filePath = selectedImagePath; 
       } else if (filemanagerstring != null) { 
        filePath = filemanagerstring; 
       } else { 
        Toast.makeText(getApplicationContext(), "Unknown path", 
          Toast.LENGTH_LONG).show(); 
        Log.e("Bitmap", "Unknown path"); 
       } 

       if (filePath != null) { 
        Log.e("file path ","file path "+filePath); 
        GlobalValues.camefromtw="true"; 
        decodeFile(filePath); 
       } else { 
        bitmap = null; 
       } 
      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), "Internal error", 
         Toast.LENGTH_LONG).show(); 
       Log.e(e.getClass().getName(), e.getMessage(), e); 
      } 
     } 
     break;protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    switch (requestCode) { 
    case PICK_IMAGE: 
     Log.e("result code","result code"+resultCode+" result "+Activity.RESULT_OK); 
     if (resultCode == Activity.RESULT_OK) { 
      Uri selectedImageUri = data.getData(); 
      try { 
       // OI FILE Manager 
       String filemanagerstring = selectedImageUri.getPath(); 

       // MEDIA GALLERY 
       String selectedImagePath = getPath(selectedImageUri); 

       if (selectedImagePath != null) { 
        filePath = selectedImagePath; 
       } else if (filemanagerstring != null) { 
        filePath = filemanagerstring; 
       } else { 
        Toast.makeText(getApplicationContext(), "Unknown path", 
          Toast.LENGTH_LONG).show(); 
        Log.e("Bitmap", "Unknown path"); 
       } 

       if (filePath != null) { 

        decodeFile(filePath); 
       } else { 
        bitmap = null; 
       } 
      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), "Internal error", 
         Toast.LENGTH_LONG).show(); 
       Log.e(e.getClass().getName(), e.getMessage(), e); 
      } 
     } 
     break; 
    default: 
    } 

이미지 지금 당신이 그것을 사용할 수있는 파일 경로에 받았다 여기 시작합니다

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

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE: 
     if(resultCode == RESULT_OK){ 
      Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex); 
      cursor.close(); 


      Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); 

      //Now do whatever processing you want to do on it. 
     } 
    } 
} 
0

갤러리에서 이미지를 선택하기 위해 다음 코드를 사용하여 나는이 세트를 사용하고있다. ImageView

public void decodeFile(String filePath) { 

    // Decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, o); 

    // The new size we want to scale to 
    final int REQUIRED_SIZE = 1024; 

    // Find the correct scale value. It should be the power of 2. 
    int width_tmp = o.outWidth, height_tmp = o.outHeight; 
    int scale = 1; 
    while (true) { 
     if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
      break; 
     width_tmp /= 2; 
     height_tmp /= 2; 
     scale *= 2; 
    } 

    // Decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize = scale; 
    bitmap = BitmapFactory.decodeFile(filePath, o2); 

    image.setImageBitmap(bitmap); 

} 
관련 문제