Android : Running app has been crash

on Sunday, September 14, 2014


I am a beginner of android development. I am trying to create an image gallery. I have little complete. But my system has been crashed when relaunch the gallery app second time and that time, app has been crash. Suppose I click on the galley app first time and the app is run on the system. After that I close the app. Second time I click on the app but app doesn't run the system and app has been crash. Third time app run properly. This is my problem. I need help.


My code is below


MainActivity.java



public class MainActivity extends Activity {

private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
File file = null;

AlertDialog.Builder alertDialog2;

int deleteindex=0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

show();

final Button selectBtn = (Button) findViewById(R.id.selectBtn);
final Button shareBtn = (Button) findViewById(R.id.btnShare);



selectBtn.setOnClickListener(new OnClickListener() {



public void onClick(View v)
{
final int len = thumbnailsselection.length;

int cnt = 0;
//final String selectImages = "";

alertDialog2 = new AlertDialog.Builder( MainActivity.this);
for ( int i =0; i<len; i++)
{
if (thumbnailsselection[i])
{
cnt++;
String selectImages=""; ;

selectImages+= arrPath[i] +" | ";


// Setting Dialog Title
alertDialog2.setTitle("Confirm Delete...");

// Setting Dialog Message
alertDialog2.setMessage("Are you sure you want delete this file?");

deleteindex=i;
// Setting Positive "Yes" Btn
alertDialog2.setPositiveButton("YES", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog

file= new File(arrPath[deleteindex]);
if(file.exists())
{
file.delete();
//Log.v("roni", selectImages);
//arrPath[i] = null;
//selectImages = null;
//Log.v("roni", arrPath[i]);
}

Toast.makeText(getApplicationContext(), " Images Deleted ",Toast.LENGTH_SHORT).show();


show();

}
});
// Setting Negative "NO" Btn
alertDialog2.setNegativeButton("NO", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
Toast.makeText(getApplicationContext(),
"You clicked on NO", Toast.LENGTH_SHORT)
.show();

dialog.cancel();
}
});
}

}
if (cnt == 0){
Toast.makeText(getApplicationContext(),
"Please select at least one image",
Toast.LENGTH_SHORT).show();
} else
{
//show();


alertDialog2.show();


// Log.v("SelectedImages", selectImages);

}

}

});

shareBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//int id = v.getId();


Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);

final int len = thumbnailsselection.length;
int cnt = 0;
ArrayList<Uri> imageUris = new ArrayList<Uri>();
Uri path= null;


String selectImages = "";
for (int i =0; i<len; i++)
{
if (thumbnailsselection[i])
{
cnt++;
selectImages = selectImages + arrPath[i] +" | ";
Log.v("roni", selectImages);
path = Uri.parse(arrPath[i]);
imageUris.add(path);

}
}
if (cnt == 0){
Toast.makeText(getApplicationContext(),
"Please select at least one image",
Toast.LENGTH_SHORT).show();
} else
{
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));


startActivity(shareIntent);

Log.v("roni", selectImages);

Toast.makeText(getApplicationContext(),cnt +" Images shared ",Toast.LENGTH_SHORT).show();
Log.d("SelectedImages", selectImages);

}
}
});

show();
}


public void show() {

//stored data
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;


Cursor imagecursor = managedQuery(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.thumbnails = new Bitmap[this.count];

this.arrPath = new String[this.count];

this.thumbnailsselection = new boolean[this.count];


//move image
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MINI_KIND, null);
arrPath[i]= imagecursor.getString(dataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);


imageAdapter = new ImageAdapter();

imagegrid.setAdapter(imageAdapter);

imagecursor.close();


}

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) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.galleryitem, null);

holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);

convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);

/*/holder.imageview.setOnLongClickListener(new OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub

ImageView image = (ImageView)v;
int id = image.getId();
if (thumbnailsselection[id]){

image.equals( true);
thumbnailsselection[id] = true;
}


return false;
}
});*/

holder.checkbox.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.imageview.setOnClickListener(new OnClickListener() {

public void onClick(View v) {


int id = v.getId();

Intent i = new Intent(MainActivity.this, ShowImageActivity.class);
i.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");

startActivity(i);
}
});
holder.imageview.setImageBitmap(thumbnails[position]);
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}




ShowImageActivity.java



public class ShowImageActivity extends Activity {

private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
private ImageView imageView;
private String selectedImagePath;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageshow);
imageView = (ImageView) findViewById(R.id.result);


Intent intent = getIntent();
Uri data = intent.getData();
//Check If data type is Image
if (intent.getType().indexOf("image/") != -1)
{
imageView.setImageURI(data);
}

}

}


ActivityManifest



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mygallery"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
<data android:mimeType="image/*" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />

<data android:mimeType="image/*" />
</intent-filter>
</activity>
<activity android:name=".ShowImageActivity"
android:label="Image" >


<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>

</application>

</manifest>

0 comments:

Post a Comment