IT_Programming/Android_Java

intent 간의 data 가 100kb 가 넘어갈 경우 !

JJun ™ 2011. 12. 15. 14:40

------------------------------------------------------------------------------------------------

출처: http://zippiest.tistory.com/46

------------------------------------------------------------------------------------------------

 

Gallery 어플에 있는 이미지를 Crop 하여 가져오는 방법으로 intent 간에 data 가 100kb 가 넘어갈 경우

에러가 발생하므로 아래와같이 파일로 저장후 읽어들여 가져옴.

 

 

private static final String TEMP_PHOTO_FILE = "temporary_holder.jpg";

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent
.setType("image/*");
photoPickerIntent
.putExtra("crop", "true");
photoPickerIntent
.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
photoPickerIntent
.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult
(photoPickerIntent, REQ_CODE_PICK_IMAGE);

private Uri getTempUri() {
     return Uri.fromFile(getTempFile());
}

private File getTempFile()

{
     if (isSDCARDMounted())

     {
          File f = new File(Environment.getExternalStorageDirectory(),TEMP_PHOTO_FILE);
          try {
                 f
.createNewFile();
          } catch (IOException e) { }


          return f;
     }

     else

     {
         return null;
     }
}

private boolean isSDCARDMounted()

{
     String status = Environment.getExternalStorageState();
     if (status.equals(Environment.MEDIA_MOUNTED)) {
         return true;
     }

 

     return false;
}

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)

                { 
                      if (imageReturnedIntent!=null)

                      {
                          File tempFile = getTempFile();
                          String filePath= Environment.getExternalStorageDirectory() +

                                                                                         "/temporary_holder.jpg";
                          System.out.println("path "+filePath); 


                          Bitmap selectedImage = BitmapFactory.decodeFile(filePath); 
                          _image
= (ImageView) findViewById(R.id.image);
                          _image
.setImageBitmap(selectedImage ); 
                     } 
               }
      }

}

 

 

 


 

더보기

 출처: http://warmz.tistory.com/400


[main.xml]

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
      android:orientation="vertical"
      android:layout_width="fill_parent"    
      android:layout_height="fill_parent"
      android:weightSum="1">    

 

 <Button android:layout_width="wrap_content"       
       android:layout_height="wrap_content"
       android:id="@+id/button"       
       android:text="GetImage"></Button>    
  
 <ImageView android:layout_height="wrap_content"
      android:id="@+id/imageView"       
      android:layout_weight="0.65"
      android:layout_width="284dp"></ImageView>
  
</LinearLayout> 

 

 

 

 

[ LoadGallery2Activity .java]

 

import java.io.File;
import java.io.IOException;  
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;  

 

public class LoadGallery2Activity extends Activity
{   
      /** Called when the activity is first created. */     
      private static final String TEMP_PHOTO_FILE = "temp.jpg";       // 임시 저장파일   
      private static final int REQ_CODE_PICK_IMAGE = 0;      
 
      @Override   
      public void onCreate(Bundle savedInstanceState)
      {        
           super.onCreate(savedInstanceState);        
           setContentView(R.layout.main);           
  
           Button bt = (Button) findViewById(R.id.button);          
           bt.setOnClickListener(new onClickListener()
           {            
                 @Override           
                 public void onClick(View v)
                 {                
                       Intent intent = new Intent(Intent.ACTION_GET_CONTENT,      // 또는 

                       ACTION_PICK                         
                       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                 

 

                   intent.setType("image/*");            // 모든 이미지                
                   intent.putExtra("crop", "true");        // Crop기능 활성화                 
                   intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri()); // 임시파일생성                
                   intent.putExtra("outputFormat",         // 포맷방식                        
                                             Bitmap.CompressFormat.JPEG.toString());                   
    

         // REQ_CODE_PICK_IMAGE == requestCode 
       startActivityForResult(intent, REQ_CODE_PICK_IMAGE);  
   }         

});       

 

}   
 
 /** 임시 저장 파일의 경로를 반환 */   
 private Uri getTempUri()
 {        
       return Uri.fromFile(getTempFile());    
 }     
 
 /** 외장메모리에 임시 이미지 파일을 생성하여 그 파일의 경로를 반환  */   
 private File getTempFile()
 {        
       if (isSDCARDMOUNTED())
       {             

                File f = new File(Environment.getExternalStorageDirectory(), // 외장메모리 경로
                               TEMP_PHOTO_FILE);            
   
                try 
                {                
                          f.createNewFile();      // 외장메모리에 temp.jpg 파일 생성             
                }
                catch (IOException e) {  }               
   
                return f;         
       }
       else
       {
               return null;     
       }       

}
 
 /** SD카드가 마운트 되어 있는지 확인 */   
 private boolean isSDCARDMOUNTED()
 {        
         String status = Environment.getExternalStorageState();         
         if (status.equals(Environment.MEDIA_MOUNTED))  {          
                return true;           

         }
  
         return false;    
 }      
 
 /** 다시 액티비티로 복귀하였을때 이미지를 셋팅 */   
 protected void onActivityResult(int requestCode, int resultCode, Intent imageData)
 {        
         super.onActivityResult(requestCode, resultCode, imageData);       

    
         switch (requestCode)
         {        
              case REQ_CODE_PICK_IMAGE:            
                      if (resultCode == RESULT_OK)
                      {                
                           if (imageData != null)
                           {                    
                                 String filePath = Environment.getExternalStorageDirectory() +

                                                                         "/temp.jpg";                       
                                 System.out.println("path" + filePath); // logCat                       


                                 // temp.jpg파일을 Bitmap으로 디코딩한다. 
                                 Bitmap selectedImage = BitmapFactory.decodeFile

                                                                                                   (filePath);

                                 ImageView _image = (ImageView) findViewById

                                                                        (R.id.imageView);                     

 

                                 // temp.jpg파일을 이미지뷰에 씌운다. 
                                 _image.setImageBitmap(selectedImage);                       
                            }            
                      }            
               break;        
         }    
  } 

 

}