The 1 who shall not be named The 1 who shall not be named - 5 months ago 21
Android Question

How can I save my current shown image when I click a imageview(button)?

I have an ImageView that shows 10 different images (you can swipe trough them)
I have Like and a Dislike button,
I want to achieve the following:

When I click on the imageview(button) to save the current shown image in the sd card and to retrieve these saved images and display them in a different activity (in a gridview).

I've read allot of how people tried to fix it and they were usually able.
But my situation is a bit different due to the fact that the images that are being displayed are stacked on top of eachother, I can also swipe them.

I would greatfully appreciate some example code of how I should face this problem.

How I think I should face this:

> 1. Imageview imageView = (Imageview) findViewById(R.id.imgLike);
> 2. setOnClickListener for the imgLike (this is the imageview "like" button)
> 3. Save the image everytime the user "likes" the image and save it in the SD card
> 4. In the next Activity load the saved images from the SD card and display them in a gridview


I would hope to get some help with step 3 and 4. Please do point it out.

Am I thinking correct or am I missing something?

thnx in advance

EDIT.1

package com.example.sick.foodinspiration;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
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.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.firebase.client.Firebase;
import com.firebase.tubesock.Base64;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.rk.lib.view.SwipeView;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MainActivity extends Activity implements
SwipeView.OnCardSwipedListener {

private final static int CARDS_MAX_ELEMENTS = 5;

private FrameLayout contentLayout;
private SwipeView mSwipeView;
private Firebase mRef;
public ImageView imageview;

private int[] meals = {
R.drawable.gevulde_avocados_met_ei,
R.drawable.pasta_met_spinazie_en_garnalen,
R.drawable.griekse_aardappelen,
R.drawable.pasta_met_spinazie_en_gorgonzolasaus,
R.drawable.zalm_spinazie,
R.drawable.pasta_bloemkoolsaus,
R.drawable.paella,
R.drawable.zweedseballen_salade,
R.drawable.rode_curry_met_runderreepjes,
R.drawable.tonijnburger};

private int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_view_demo);
contentLayout = (FrameLayout) findViewById(R.id.contentLayout);


// Add the swipe view
mSwipeView = new SwipeView(this, R.id.imgSwipeLike, R.id.imgSwipeNope,
this);
contentLayout.addView(mSwipeView);

// Adding the cards initially with the maximum limits of cards.
for (int i = 0; i < CARDS_MAX_ELEMENTS; i++) {
addCard(i);
}
}

/**
* On clicked view.
*
* @param clickedView
* the clicked view
*/
public void onClickedView(View clickedView) {
switch (clickedView.getId()) {
case R.id.imgDisLike: {
mSwipeView.dislikeCard();
break;
}

case R.id.imgLike: {

imageview.setDrawingCacheEnabled(true);
imageview.buildDrawingCache();
Bitmap bm=imageview.getDrawingCache();

OutputStream fOut = null;
Uri outputFileUri;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + "folder_name" + File.separator);
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}

try {
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {

}
mSwipeView.likeCard();
break;
}
default:
break;
}
}

@Override
public void onLikes() {

System.out.println("An Card removed");
// Add a card if you needed after any previous card swiped
addCard(0);
}

@Override
public void onDisLikes() {
System.out.println("An Card removed");
// Add a card if you needed after any previous card swiped
addCard(0);
}

@Override
public void onSingleTap() {

}

/**
* Adds the card to the swipe.
*/

private void addCard(int position) {
final View cardView = LayoutInflater.from(this).inflate(
R.layout.item_swipe_view, null);
final ImageView imgMeals = (ImageView) cardView
.findViewById(R.id.imgMeals);
imgMeals.setImageResource(meals[count]);
count++;
if (count == meals.length) {
count = 0;
}
// Add a card to the swipe view..
mSwipeView.addCard(cardView, position);

// Create OnClickListener for the CookBookActivity
// Declare Button for the Cookbook
Button btn = (Button) findViewById(R.id.button3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CookbookActivity.class));
}
});

// Check Authentication
mRef = new Firebase(com.example.sick.foodinspiration.Constants.FIREBASE_URL);
if (mRef.getAuth() == null) {
loadLoginView();
}
}

private void loadLoginView() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}

}


UPDATE 16-6-2016
Im back with an update,
I've managed to succesfully add an image in the gallery. I used to have a problem with the folder NOT being showed, but I fixed it with;

MediaScannerConnection.scanFile(this, new String[] { sdImageMainDirectory.getAbsolutePath() }, null, null);


So now the Image that im addressing, is being place in a new folder.
So far so good, however
Problem 2
I'm getting 5 hardcoded images from the drawable folder and want to show them in CardView.(So stacked on top of eachother)
Then I want to save these images in the SD card when the like button is being pressed or the swipe function is being used.

Relevant code for this problem

private int[] meals = {
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e};

private int count = 0;


AND

private void addCard(int position) {
final View cardView = LayoutInflater.from(this).inflate(
R.layout.item_swipe_view, null);
final ImageView imgMeal = (ImageView) cardView
.findViewById(R.id.imgMeals);
imgMeal.setImageResource(meals[count]);
count++;
if (count == meals.length) {
count = 0;
}
// Add a card to the swipe view..
mSwipeView.addCard(cardView, position);


The way I think I should face this, is the following:

> so *meals* is already being initialized so I dont have to do this.
> I have to make an OnClickListener under the
private void addCard(int position)
> Implement the code Neo was kind to share with us.
> Put Neo's code in the onClick (Like) function
> Make sure the code listens to "meals" because this is the final ImageView


I hope im looking at this problem correctly.
If anyone can help me with this problem I would kindly appreciate it!

And thnx Neo for your help so far!

Neo Neo
Answer

Try this code:

imageview.setDrawingCacheEnabled(true); //Add this line.
imageview.buildDrawingCache();
Bitmap bm=imageview.getDrawingCache();

To save it in a file:

OutputStream fOut = null;
    Uri outputFileUri;
    try {
         File root = new File(Environment.getExternalStorageDirectory()
          + File.separator + "folder_name" + File.separator);
         root.mkdirs();
         File sdImageMainDirectory = new File(root, "myPicName.jpg");
         outputFileUri = Uri.fromFile(sdImageMainDirectory);
         fOut = new FileOutputStream(sdImageMainDirectory);
    } catch (Exception e) {
         Toast.makeText(this, "Error occured. Please try again later.",
         Toast.LENGTH_SHORT).show();
    }

    try {
        bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) 
}

And don't forget to add permission in manifest:

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

Hope it will help you :)