2015-01-23 5 views
-1

내 안드로이드 레이아웃에서 gridview를 디자인했습니다. 내 장치에서 이미지를 선택하여 그리드 항목으로 표시하고 있습니다. 해당 응용 프로그램을 실행하려고 할 때 응용 프로그램이 처음 올바르게 실행됩니다. 그러나 그림 추가 단추를 클릭하여 그리드 뷰에 그림을 추가하려고 할 때 강제로 내 응용 프로그램을 닫습니다.Android에서 애플리케이션 자동 종료를 해결하는 방법은 무엇입니까?

01-23 16:40:56.762: E/AndroidRuntime(21288): FATAL EXCEPTION: main 
01-23 16:40:56.762: E/AndroidRuntime(21288): Process: com.example.imagepicfromgallery, PID: 21288 
01-23 16:40:56.762: E/AndroidRuntime(21288): java.lang.OutOfMemoryError 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at com.example.imagepicfromgallery.CustomList.getView(CustomList.java:33) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.AbsListView.obtainView(AbsListView.java:2300) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.GridView.makeAndAddView(GridView.java:1345) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.GridView.makeRow(GridView.java:345) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.GridView.fillDown(GridView.java:287) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.GridView.fillFromTop(GridView.java:421) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.GridView.layoutChildren(GridView.java:1233) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.AbsListView.onLayout(AbsListView.java:2149) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.View.layout(View.java:15125) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.ViewGroup.layout(ViewGroup.java:4862) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.LinearLayout.onLayout(LinearLayout.java:1651) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.View.layout(View.java:15125) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.ViewGroup.layout(ViewGroup.java:4862) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.FrameLayout.onLayout(FrameLayout.java:450) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.View.layout(View.java:15125) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.ViewGroup.layout(ViewGroup.java:4862) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1888) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1742) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.LinearLayout.onLayout(LinearLayout.java:1651) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.View.layout(View.java:15125) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.ViewGroup.layout(ViewGroup.java:4862) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.widget.FrameLayout.onLayout(FrameLayout.java:450) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.View.layout(View.java:15125) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.ViewGroup.layout(ViewGroup.java:4862) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2323) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2029) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.Choreographer.doCallbacks(Choreographer.java:591) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.Choreographer.doFrame(Choreographer.java:560) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.os.Handler.handleCallback(Handler.java:808) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.os.Handler.dispatchMessage(Handler.java:103) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.os.Looper.loop(Looper.java:193) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at android.app.ActivityThread.main(ActivityThread.java:5292) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at java.lang.reflect.Method.invokeNative(Native Method) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at java.lang.reflect.Method.invoke(Method.java:515) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
01-23 16:40:56.762: E/AndroidRuntime(21288): at dalvik.system.NativeStart.main(Native Method) 

MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener{ 

//private LinearLayout lnrImages; 
GridView lnrImages; 
private Button btnAddPhots; 
private Button btnSaveImages; 
private ArrayList<String> imagesPathList; 
private Bitmap yourbitmap; 
private Bitmap resized; 
private final int PICK_IMAGE_MULTIPLE =1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    lnrImages = (GridView) findViewById(R.id.grdImages); 
    btnAddPhots = (Button)findViewById(R.id.btnAddPhots); 
    btnSaveImages = (Button)findViewById(R.id.btnSaveImages); 
    btnAddPhots.setOnClickListener(this); 
    btnSaveImages.setOnClickListener(this); 
} 
@Override 
public void onClick(View view) { 
    switch (view.getId()) { 
     case R.id.btnAddPhots: 
      Intent intent = new Intent(MainActivity.this,CustomPhotoGalleryActivity.class); 
      startActivityForResult(intent,PICK_IMAGE_MULTIPLE); 
      break; 
     case R.id.btnSaveImages: 
      if(imagesPathList !=null){ 
       if(imagesPathList.size()>1) { 
        Toast.makeText(MainActivity.this, imagesPathList.size() + " no of images are selected", Toast.LENGTH_SHORT).show(); 
       }else{ 
        Toast.makeText(MainActivity.this, imagesPathList.size() + " no of image are selected", Toast.LENGTH_SHORT).show(); 
       } 
      }else{ 
       Toast.makeText(MainActivity.this," no images are selected", Toast.LENGTH_SHORT).show(); 
      } 
      break; 
    } 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     if(requestCode == PICK_IMAGE_MULTIPLE){ 
      imagesPathList = new ArrayList<String>(); 
      String[] imagesPath = data.getStringExtra("data").split("\\|"); 
      CustomList adapter = new 
        CustomList(MainActivity.this,imagesPath); 
      lnrImages.setAdapter(adapter); 
     } 
    } 

} 

} 

CustomList.java

public class CustomList extends ArrayAdapter<String>{ 

private final Activity context; 
private final String[] web; 

public CustomList(Activity context, 
String[] web) { 
super(context, R.layout.list_single, web); 
this.context = context; 
this.web = web; 


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

ImageView imageView = (ImageView) rowView.findViewById(R.id.img); 


imageView.setImageBitmap(BitmapFactory.decodeFile(web[position])); 
return rowView; 
} 
} 

CustomPhotoGalleryActivity.java

public class CustomPhotoGalleryActivity extends Activity { 

private GridView grdImages; 
private Button btnSelect; 

private ImageAdapter imageAdapter; 
private String[] arrPath; 
private boolean[] thumbnailsselection; 
private int ids[]; 
private int count; 


/** 
* Overrides methods 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.custom_gallery); 
    grdImages= (GridView) findViewById(R.id.grdImages); 
    btnSelect= (Button) findViewById(R.id.btnSelect); 

    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }; 
    final String orderBy = MediaStore.Images.Media._ID; 
    @SuppressWarnings("deprecation") 
    Cursor imagecursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy); 
    int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID); 
    this.count = imagecursor.getCount(); 
    this.arrPath = new String[this.count]; 
    ids = new int[count]; 
    this.thumbnailsselection = new boolean[this.count]; 
    for (int i = 0; i < this.count; i++) { 
     imagecursor.moveToPosition(i); 
     ids[i] = imagecursor.getInt(image_column_index); 
     int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA); 
     arrPath[i] = imagecursor.getString(dataColumnIndex); 
    } 

    imageAdapter = new ImageAdapter(); 
    grdImages.setAdapter(imageAdapter); 
    imagecursor.close(); 


    btnSelect.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      final int len = thumbnailsselection.length; 
      int cnt = 0; 
      String selectImages = ""; 
      for (int i = 0; i < len; i++) { 
       if (thumbnailsselection[i]) { 
        cnt++; 
        selectImages = selectImages + arrPath[i] + "|"; 
       } 
      } 
      if (cnt == 0) { 
       Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show(); 
      } else { 

       Log.d("SelectedImages", selectImages); 
       Intent i = new Intent(); 
       i.putExtra("data", selectImages); 
       setResult(Activity.RESULT_OK, i); 
       finish(); 
      } 
     } 
    }); 
} 
@Override 
public void onBackPressed() { 
    setResult(Activity.RESULT_CANCELED); 
    super.onBackPressed(); 

} 

/** 
* Class method 
*/ 

/** 
* This method used to set bitmap. 
* @param iv represented ImageView 
* @param id represented id 
*/ 

private void setBitmap(final ImageView iv, final int id) { 

    new AsyncTask<Void, Void, Bitmap>() { 

     @Override 
     protected Bitmap doInBackground(Void... params) { 
      return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null); 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      super.onPostExecute(result); 
      iv.setImageBitmap(result); 
     } 
    }.execute(); 
} 


/** 
* List adapter 
* @author tasol 
*/ 

public class ImageAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 

    public ImageAdapter() { 
     mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return count; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = mInflater.inflate(R.layout.custom_gallery_item, null); 
      holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb); 
      holder.chkImage = (CheckBox) convertView.findViewById(R.id.chkImage); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.chkImage.setId(position); 
     holder.imgThumb.setId(position); 
     holder.chkImage.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       CheckBox cb = (CheckBox) v; 
       int id = cb.getId(); 
       if (thumbnailsselection[id]) { 
        cb.setChecked(false); 
        thumbnailsselection[id] = false; 
       } else { 
        cb.setChecked(true); 
        thumbnailsselection[id] = true; 
       } 
      } 
     }); 
     holder.imgThumb.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       int id = holder.chkImage.getId(); 
       if (thumbnailsselection[id]) { 
        holder.chkImage.setChecked(false); 
        thumbnailsselection[id] = false; 
       } else { 
        holder.chkImage.setChecked(true); 
        thumbnailsselection[id] = true; 
       } 
      } 
     }); 
     try { 
      setBitmap(holder.imgThumb, ids[position]); 
     } catch (Throwable e) { 
     } 
     holder.chkImage.setChecked(thumbnailsselection[position]); 
     holder.id = position; 
     return convertView; 
    } 
} 


/** 
* Inner class 
* @author tasol 
*/ 
class ViewHolder { 
    ImageView imgThumb; 
    CheckBox chkImage; 
    int id; 
} 
} 

activity_main.xml

,363,210
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.imagepicfromgallery.MainActivity" > 

<GridView 
    android:id="@+id/grdImages" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:columnWidth="90dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center"/> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <Button 
     android:id="@+id/btnAddPhots" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Add Phots"/> 

    <Button 
     android:id="@+id/btnSaveImages" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Save"/> 
</LinearLayout> 

custom_gallery_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/FrameLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ImageView 
    android:id="@+id/imgThumb" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

<CheckBox 
    android:id="@+id/chkImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="top|right"/> 
</FrameLayout> 

custom_gallery.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
android:orientation="vertical"> 
<GridView 
    android:id="@+id/grdImages" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:columnWidth="90dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center"/> 

<Button 
    android:id="@+id/btnSelect" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Select" 
    android:layout_marginTop="5dp"/> 

</LinearLayout> 

list_single.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/img" 
     android:layout_width="50dp" 
     android:layout_height="50dp"/> 
</TableLayout> 

답변

2

당신이 있는지 확인 이미지를 처리 ​​할 때 메모리 부족하고

: 읽기 및 편집 질문에 추가하려면 AndroidManifest.xml을

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

감사합니다 선생님 아래 AndroidManifest를에 android:largeHeap="true"를 추가,하지만 지금 난 내 로그 캣에 새로운 오류가 발생하고 있습니다. 01-23 14 : 31 : 37.077 : E/(15977) : appName = com.example.imagepicfromgallery, acAppName =/system/bin/surfaceflinger 01-23 14 : 31 : 37.077 : E/(15977) : 0 01-23 14 : 31 : 37.077 : E/(15977) : appName = com.example.imagepicfromgallery, acAppName =/system/bin/surfaceflinger 01-23 14 : 31 : 37.077 : E/(15977) : 0 –

+0

@SritamJagadev는 예외 전체를 게시하십시오. – arnfada

+0

01-23 15 : 28 : 04.294 : E/(17900) : appName = com.example.imagepicfromgallery, acAppName =/system/bin/surfaceflinger 01-23 15 : 28 : 04.294 : E/(17900) : 0 01-23 15 : 28 : 04.294 : E/(17900) : appName = com.example.imagepicfromgallery, acAppName =/system/bin/surfaceflinger 01-23 15 : 28 : 04.294 : E/(17900) : 0 –

2

외부 저장소에 쓸 수 있도록 권한을 사용 가능. 안드로이드 에뮬레이터를 사용하는 경우 일반적으로 큰 메모리 1GB +를 사용하도록 구성되어 있는지 확인하십시오.

Android discusses handling large bitmaps here

Here's a topic stackoverflow on how to handle large bitmaps

또한 시도하고 <application

+0

선생님 내 휴대 전화 (iball 슬라이드 6095-q700)를 사용하여 프로그램을 디버깅하고 있습니다. 또한 8 기가 바이트 이상의 빈 공간을 가지고 있지만 여전히 동일한 문제에 직면 해있는 장치. –

관련 문제