Newbie: Edited photos not saving/showing up in Phone Gallery

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Oct 8, 2017 6:05:02 PM via Website

Hi guys, I am Daniel, I am just getting started with android development and I have done some simple apps one of which is a photo editor, Now I have added a MY COLLECTION tab on the app where the users can see the photos they just edited but I am not sure why the photos are not showing up in the phone gallery. Here is My code (part of the main code)

 Done.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Effects.getInstance().playSound(Effects.SOUND_1);
            try {
                ccmCurrentView.setInEdit(false);
            } catch (Exception e) {
                e.printStackTrace();
            }

// tv_appName.setVisibility(View.VISIBLE);
Intent localIntent1 = new Intent(getApplicationContext(), Finalactivity.class);
draglout.setDrawingCacheEnabled(true);
draglout.buildDrawingCache(true);
fbitmap = Bitmap.createBitmap(draglout.getDrawingCache());

            storeImage(fbitmap);
            draglout.setDrawingCacheEnabled(false);
            startActivity(localIntent1);
        }
    });

    @SuppressWarnings("deprecation")
    BitmapDrawable localBitmapDrawable = new BitmapDrawable(HomeActivity.thumbnail);
    frame.setImageDrawable(localBitmapDrawable);
}

public static File pictureFile;
public static Uri shareImageURI;
public static String shareImageFilePath = "";

private void storeImage(Bitmap image) {
    pictureFile = getOutputMediaFile();
    if (pictureFile == null) {
        Log.e("===",
                "Error creating media file, check storage permissions: ");// e.getMessage());
        return;
    }
    try {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        image.compress(Bitmap.CompressFormat.JPEG, 90, fos);
        fos.flush();
        fos.close();

        Toast.makeText(getApplicationContext(), "Image Saved Successfully.", Toast.LENGTH_SHORT).show();

    } catch (FileNotFoundException e) {
        Log.e("===", "File not found: " + e.getMessage());
        Toast.makeText(getApplicationContext(), "Image not saved", Toast.LENGTH_SHORT).show();

    } catch (IOException e) {
        Log.e("===", "Error accessing file: " + e.getMessage());
        Toast.makeText(getApplicationContext(), "Image not saved", Toast.LENGTH_SHORT).show();
    }

    boolean flag = pictureFile.exists();
    Uri uri = null;
    if (flag) {
        uri = Uri.fromFile(pictureFile);
        shareImageURI = uri;
    }
    shareImageFilePath = getRealPathFromURI(uri);
    Log.e("===", "shareImageURI " + shareImageURI);
}

/**
 * Create a File for saving an image or video
 */
private File getOutputMediaFile() {

    // using Environment.getExternalStorageState() before doing this.

// File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
// + "/Android/data/"
// + getApplicationContext().getPackageName()
// + "/Files" + Global.AppFolder);

    File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "/" + Global.AppFolder);

    // Create the storage directory if it does not exist
    if (!root.exists()) {
        if (!root.mkdirs()) {
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date());
    File mediaFile;
    String mImageName = "MI_" + timeStamp + ".jpg";
    mediaFile = new File(root.getAbsolutePath(), mImageName);
    return mediaFile;
}

private String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    if (cursor == null) {
        return uri.getPath();
    } else {
        cursor.moveToFirst();
        String s = cursor.getString(cursor.getColumnIndex("_data"));
        cursor.close();
        return s;
    }
}

public void gatedata(Intent paramIntent) {
    addStickerView((Integer) paramIntent.getExtras().get("wall_id"));
}

protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent) {
    if ((paramInt1 == 1) && (paramInt2 == -1)) {
        index = paramIntent.getIntExtra("index1", 0);
        if (index == 1)
            gatedata(paramIntent);
        if (index == 2)
            gatedata(paramIntent);
        if (index == 3)
            gatedata(paramIntent);
        if (index == 4)
            gatedata(paramIntent);
        if (index == 5)
            gatedata(paramIntent);
        if (index == 6)
            gatedata(paramIntent);
        if (index == 7)
            gatedata(paramIntent);
        if (index == 8)
            gatedata(paramIntent);
        if (index == 9)
            gatedata(paramIntent);
        if (index == 10)
            gatedata(paramIntent);
        if (index == 11)
            gatedata(paramIntent);
        if (index == 12)
            gatedata(paramIntent);
        if (index == 13)
            gatedata(paramIntent);
        if (index == 14)
            gatedata(paramIntent);
        if (index == 15)
            gatedata(paramIntent);
        if (index == 16)
            gatedata(paramIntent);
    }
    super.onActivityResult(paramInt1, paramInt2, paramIntent);
}

public void onBackPressed() {
    finish();
}

private void addStickerView(int id) {
    final bmpStickerView bmpstickerView = new bmpStickerView(this);
    bmpstickerView.setImageResource(id);

    bmpstickerView.setOperationListener(new bmpStickerView.OperationListener() {
        @Override
        public void onDeleteClick() {
            ccmViews.remove(bmpstickerView);
            ccmContentRootView.removeView(bmpstickerView);
        }

        @Override
        public void onEdit(bmpStickerView ccstickerView) {
            ccmCurrentView.setInEdit(false);
            ccmCurrentView = ccstickerView;
            ccmCurrentView.setInEdit(true);
        }

        @Override
        public void onTop(bmpStickerView ccstickerView) {
            int position = ccmViews.indexOf(ccstickerView);
            if (position == ccmViews.size() - 1) {
                return;
            }
            bmpStickerView stickerTemp = (bmpStickerView) ccmViews.remove(position);
            ccmViews.add(ccmViews.size(), stickerTemp);
        }

    });
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    ccmContentRootView.addView(bmpstickerView, lp);
    ccmViews.add(bmpstickerView);
    setCurrentEdit(bmpstickerView);
}

private void setCurrentEdit(bmpStickerView ccstickerView) {
    if (ccmCurrentView != null) {
        ccmCurrentView.setInEdit(false);
    }

    ccmCurrentView = ccstickerView;
    ccstickerView.setInEdit(true);
}

@Override
protected void onResume() {
    super.onResume();
    tv_appName.setVisibility(View.GONE);
}

}