Switching to another activity takes longer in android
On pressing the camera button (addImage) I move the screen to another action. In this case, it takes a long time to open a new activity. Can anyone tell me what the problem is and how to solve it?
public class Gallery_Activity extends Activity {
AsyncTaskLoadFiles myAsyncTaskLoadFiles;
private ProgressDialog pDialog;
public class AsyncTaskLoadFiles extends AsyncTask<Void, String, Void> {
File targetDirector;
ImageAdapter myTaskAdapter;
ImageView imageDetail;
public AsyncTaskLoadFiles(ImageAdapter adapter) {
myTaskAdapter = adapter;
}
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(Gallery_Activity.this);
pDialog.setIndeterminateDrawable(getResources().getDrawable(R.anim.progress));
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/Partypad/";
targetDirector = new File(targetPath);
myTaskAdapter.clear();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
File[] files = targetDirector.listFiles();
Arrays.sort(files);
for (File file : files) {
publishProgress(file.getAbsolutePath());
if (isCancelled()) break;
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
myTaskAdapter.add(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
myTaskAdapter.notifyDataSetChanged();
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
byte[] _image;
int _id;
public ImageAdapter(Context c) {
mContext = c;
}
void add(String path) {
itemList.add(path);
}
void clear() {
itemList.clear();
}
void remove(int index){
itemList.remove(index);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
ImageView imageView;
if (convertView == null) { // if it not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
convertView = imageView;
holder = new ViewHolder();
holder.image = imageView;
holder.position = position;
convertView.setTag(holder);
} else {
//imageView = (ImageView) convertView;
holder = (ViewHolder) convertView.getTag();
((ImageView)convertView).setImageBitmap(null);
}
//Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220, 220);
// Using an AsyncTask to load the slow images in a background thread
new AsyncTask<ViewHolder, Void, Bitmap>() {
private ViewHolder v;
@Override
protected Bitmap doInBackground(ViewHolder... params) {
v = params[0];
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220, 220);
return bm;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
v.image.setImageBitmap(result);
}
}.execute(holder);
return convertView;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height
/ (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
class ViewHolder {
ImageView image;
int position;
}
}
ImageAdapter myImageAdapter;
Button addImage;
GridView gridview;
ImageView bigimg;
ImageView img;
Button share,reload,back;
Bitmap bmp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.gallery_view);
gridview = (GridView) findViewById(R.id.gridview);
myImageAdapter = new ImageAdapter(this);
gridview.setAdapter(myImageAdapter);
addImage = (Button) findViewById(R.id.btnAdd);
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent add = new Intent(Gallery_Activity.this,Camera_Activity.class);
startActivity(add);
}
});
back = (Button) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent add = new Intent(Gallery_Activity.this,Gallery_Activity.class);
startActivity(add);
//gridview.setVisibility(View.VISIBLE);
}
});
share = (Button) findViewById(R.id.share);
reload = (Button) findViewById(R.id.reload);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img.setImageBitmap(bmp);
Uri bmpUri = getLocalBitmapUri(img);
if (bmpUri != null) {
// Construct a ShareIntent with link to image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Shared via PartyPad");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Shared via PartyPad https://play.google.com/store/apps/details?id=com.appsinbox.partypad&hl=en");
shareIntent.setType("image/*");
// Launch sharing dialog for image
startActivity(Intent.createChooser(shareIntent, "Share Party Image"));
} else {
// ...sharing failed, handle error
}
}
});
myAsyncTaskLoadFiles = new AsyncTaskLoadFiles(myImageAdapter);
myAsyncTaskLoadFiles.execute();
gridview.setOnItemClickListener(myOnItemClickListener);
Button buttonReload = (Button)findViewById(R.id.reload);
buttonReload.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
//Cancel the previous running task, if exist.
myAsyncTaskLoadFiles.cancel(true);
//new another ImageAdapter, to prevent the adapter have
//mixed files
myImageAdapter = new ImageAdapter(Gallery_Activity.this);
gridview.setAdapter(myImageAdapter);
myAsyncTaskLoadFiles = new AsyncTaskLoadFiles(myImageAdapter);
myAsyncTaskLoadFiles.execute();
}});
}
public Uri getLocalBitmapUri(ImageView imageView) {
// Extract Bitmap from ImageView drawable
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
OnItemClickListener myOnItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position,
long id) {
String prompt = "remove " + (String) parent.getItemAtPosition(position);
String prompt1 = (String) parent.getItemAtPosition(position);
System.out.println("PATH TEST "+prompt1);
//Toast.makeText(getApplicationContext(), prompt, Toast.LENGTH_SHORT).show();
bmp = BitmapFactory.decodeFile(prompt1);
img = (ImageView) findViewById(R.id.big);
gridview.setVisibility(View.GONE);
img.setVisibility(View.VISIBLE);
img.setImageBitmap(bmp);
share.setVisibility(view.VISIBLE);
back.setVisibility(view.VISIBLE);
addImage.setVisibility(view.GONE);
}
};
}
+3
source to share
No one has answered this question yet
Check out similar questions: