jeudi 16 juin 2016

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);
    }

}

Aucun commentaire:

Enregistrer un commentaire