2016-11-24 2 views
0

Ich bin fest auf den Versuch, die Galerie arbeiten zu lassen. Teilweise weil es ein Fragment ist. Was ich in diesem Fragment tun möchte, ist, Bild aufzunehmen, eine Kopie im Ordner meiner App zu machen, das Bild in meine Bildansicht zu skalieren, um es als Vorschaubild zu verwenden. Verwenden Sie dieses Bild auch in anderen Bereichen der App, und speichern Sie den Speicherort des Bildes. Ich verwende Realm, um diesen Ort zu speichern.Wählen Sie ein Bild aus der Galerie und erstellen Sie ein Miniaturbild in der Bildansicht

Vielen Dank für Ihre Hilfe

Hier ist der Code:

import android.content.Context; 
 
import android.os.Bundle; 
 
import android.support.annotation.Nullable; 
 
import android.support.v7.widget.AppCompatImageView; 
 
import android.util.Log; 
 
import android.view.LayoutInflater; 
 
import android.view.View; 
 
import android.view.ViewGroup; 
 
import android.widget.Button; 
 
import android.widget.ImageView; 
 
import android.content.Intent; 
 
import android.content.Intent; 
 
import android.database.Cursor; 
 
import android.net.Uri; 
 
import android.os.Bundle; 
 
import android.provider.MediaStore; 
 
import android.support.v7.app.AppCompatActivity; 
 
import android.support.v7.widget.Toolbar; 
 
import android.util.Log; 
 
import android.view.View; 
 

 
import com.heinrichreimersoftware.materialintro.slide.FragmentSlide; 
 

 
import io.realm.Realm; 
 

 
public class Step3 extends FragmentSlide.FragmentSlideFragment implements View.OnClickListener { 
 
    boolean STEP3_PROCEEDABLE = true; 
 
    private static final int PICK_IMAGE = 1; 
 
    AppCompatImageView imgView; 
 
    Button imgButton; 
 

 
    private OnFragmentInteractionListener mListener; 
 

 
    public Step3(){ 
 
     //Required empty public constructor 
 
    } 
 

 
    /** 
 
    * Use this factory method to create a new instance of 
 
    * this fragment using the provided parameters. 
 
    */ 
 
    public static Step3 newInstance() { 
 
     Step3 fragment = new Step3(); 
 
     return fragment; 
 
    } 
 

 
    @Nullable 
 
    @Override 
 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
 
     final View rootView = inflater.inflate(R.layout.add_routine_slide3, container, false); 
 
     imgButton = (Button) rootView.findViewById(R.id.imgButton); 
 
     imgView = (AppCompatImageView) rootView.findViewById(R.id.imgView); 
 
     imgButton = (Button) inflater.inflate(R.layout.add_routine_slide3, container, false); 
 
     imgButton.setOnClickListener(this); 
 
     return rootView; 
 
    } 
 

 
    public void onButtonPressed() { 
 
     if (mListener != null) { 
 
      mListener.onFragmentInteraction(STEP3_PROCEEDABLE); 
 
     } 
 
     Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT); 
 
     getIntent.setType("image/*"); 
 

 
     Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
 
     pickIntent.setType("image/*"); 
 

 
     Intent chooserIntent = Intent.createChooser(getIntent, "Select Image"); 
 
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent}); 
 

 
     startActivityForResult(chooserIntent, PICK_IMAGE); 
 

 
    } 
 

 
    @Override 
 
    public void onAttach(Context context) { 
 
     super.onAttach(context); 
 
     if (context instanceof OnFragmentInteractionListener) { 
 
      mListener = (OnFragmentInteractionListener) context; 
 
     } else { 
 
      throw new RuntimeException(context.toString() 
 
        + " must implement OnFragmentInteractionListener"); 
 
     } 
 
    } 
 

 

 

 
    @Override 
 
    public void onDetach() { 
 
     super.onDetach(); 
 
     mListener = null; 
 
    } 
 

 
    /** 
 
    * This interface must be implemented by activities that contain this 
 
    * fragment to allow an interaction in this fragment to be communicated 
 
    * to the activity and potentially other fragments contained in that 
 
    * activity. 
 
    * <p/> 
 
    * See the Android Training lesson <a href= 
 
    * "http://developer.android.com/training/basics/fragments/communicating.html" 
 
    * >Communicating with Other Fragments</a> for more information. 
 
    */ 
 
    public interface OnFragmentInteractionListener { 
 
     void onFragmentInteraction(boolean proceedable); 
 
    } 
 
}

Und hier ist der XML-

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:orientation="vertical" 
 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
 
    android:paddingTop="@dimen/activity_vertical_margin" 
 
    android:paddingRight="@dimen/activity_horizontal_margin" 
 
    android:paddingBottom="@dimen/activity_vertical_margin"> 
 
    <LinearLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     android:orientation="vertical" 
 
     android:layout_gravity="center" 
 
     android:gravity="center" 
 
     android:foregroundGravity="center" 
 
     android:weightSum="1"> 
 
     <TextView 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:text="Rou_Image" 
 
      android:textAppearance="?android:attr/textAppearanceLarge" 
 
      android:gravity="center" 
 
      /> 
 
     <TextView 
 
      android:paddingTop="@dimen/activity_vertical_margin" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:text="Add a picture to capture your routine." 
 
      android:textAppearance="?android:attr/textAppearanceMedium" 
 
      android:gravity="center" 
 
      /> 
 

 
     <Button 
 
      android:text="Select Image" 
 
      android:layout_width="158dp" 
 
      android:layout_height="wrap_content" 
 
      android:id="@+id/imgButton" /> 
 

 
     <AppCompatImageView 
 
      android:layout_width="137dp" 
 
      android:layout_height="wrap_content" 
 
      app:srcCompat="@android:drawable/gallery_thumb" 
 
      android:id="@+id/imgView" 
 
      android:layout_weight="0.20" /> 
 
    </LinearLayout> 
 
</LinearLayout>

Antwort

0

Sie können dafür Picasso Library auschecken. Sie können ein Bild aus dem Speicher laden und es wie unter for3st beschrieben in ImageView anzeigen.

File f = new File("path-to-file/file.png") 
Picasso.with(getActivity()).load(f).into(imageView); 

Zusätzlich können Sie verwenden es auch Ihr Bild

Picasso.with(getActivity()).load(f).resize(100,100).into(imageView); 

Und speichern Sie das Bild in Ihrer App-Verzeichnis skalieren können Sie es wie diese

verwenden
Bitmap bitmap; 
Picasso.with(getActivity()).load(f).resize(100,100).into(imageView, new Callback() { 
      @Override 
      public void onSuccess() { 
       bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); 
      } 

      @Override 
      public void onError() { 
      } 
     }); 
File directory = new File(Environment.getExternalStorageDirectory().toString()+"/MyAppFolder"); 
if(!directory.exists()){  
    directory.mkdirs(); 
} 
File file = new File(directory,new Random().nextInt(10000)+".png"); 
      try { 
       FileOutputStream fos = new FileOutputStream(file); 
       bitmap.compress(Bitmap.CompressFormat.PNG,100,fos); 
       fos.flush(); 
       fos.close(); 
      }catch (IOException e){e.printStackTrace();} 

      //Gallery Refresh 
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{Environment.getExternalStorageDirectory().toString()}, null, new MediaScannerConnection.OnScanCompletedListener() { 

       public void onScanCompleted(String path, Uri uri) { 
        Log.e("ExternalStorage", "Scanned " + path + ":"); 
        Log.e("ExternalStorage", "-> uri=" + uri); 
       } 
      }); 

Hoffnung, das hilft.

Verwandte Themen