Android - ImageView / ImageButton与图库或相机的图像

时间:2017-04-20 12:25:32

标签: android image

我知道有很多这样的帖子,但我的有点特别。

  

问题描述:

我想要一个公共类,它显示一个小对话框来选择图像源,相机或图库。选择后,ImageView将显示图像,图像将保存在app文件夹中。这个类将从许多活动中调用。所以,我有这个代码:

PhotoActivity.java

package com.app.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.List;


public class PhotoActivity {

    private Context context;

    public PhotoActivity(Context current){
        this.context = current;
    }
    //this will be called from other activities
    public void Change_Photo(final Activity a) {

        List<String> options = new ArrayList<String>();
        options.add(context.getResources().getString(R.string.camera));
        options.add(context.getResources().getString(R.string.gallery));
        options.add(context.getResources().getString(R.string.cancel));

        //Create sequence of items
        final CharSequence[] Sources = options.toArray(new String[options.size()]);
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
        dialogBuilder.setTitle(context.getResources().getString(R.string.select_image_source));
        dialogBuilder.setItems(Sources, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if (item==0) {//camera
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    a.startActivityForResult(takePicture, 0);//zero can be replaced with any action code
                }else if (item == 1) {//gallery
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    a.startActivityForResult(pickPhoto , 1);//one can be replaced with any action code
                }
            }
        });
        //Create alert dialog object via builder
        AlertDialog alertDialogObject = dialogBuilder.create();
        //Show the dialog
        alertDialogObject.show();
    }
}

我已添加清单文件:

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

其他活动有像这样的ImageView或ImageButton:

<ImageButton
      android:id="@+id/btn_picture"
      android:layout_width="140dp"
      android:layout_height="140dp"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="false"
      android:elevation="0dp"
      android:gravity="center_horizontal"
      android:src="@drawable/camera" />

在开头有一个摄像头图像,但最后会显示所选图像。

接下来必须做什么才能在此ImageButton中显示所选或拍摄的照片?

1 个答案:

答案 0 :(得分:0)

public class SongListFragment extends Fragment implements MediaController.MediaPlayerControl, AdapterView.OnItemClickListener {

private ArrayList<String> orderedSongsId = new ArrayList<String>();
private ArrayList<Song> songList;
private MusicService musicService;
private NotificationService notificationService;
private Menu menu;

private View rootView;
private ListView songListView;

private boolean musicBound = false;
private MusicController controller;
private boolean paused = false, playbackPaused = false;

private static final String LOG_TAG = SongListFragment.class.getSimpleName();
private static final String TAG = SongListFragment.class.getSimpleName();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   

LocalBroadcastManager.getInstance(this.getActivity().getApplicationContext()).registerReceiver(mMusicServiceReceiver,
            new IntentFilter(Constants.ACTION.BIND_FRAG_ACTION)); 
}

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.songs_list_fragment, container, false);
    Log.v(LOG_TAG, "Fragment view is created ------------------------------");
    songList = new ArrayList<Song>();
    getSongList();
    Collections.sort(songList, new Comparator<Song>() {
        public int compare(Song a, Song b) {
            return a.getTitle().compareTo(b.getTitle());
        }
    });
    for (int i = 0; i < songList.size(); i++) {
        orderedSongsId.add(String.valueOf(songList.get(i).getId()));
    }

    SongAdapter adapter = new SongAdapter(getActivity(), songList);
    songListView = (ListView) rootView.findViewById(R.id.song_list);
    songListView.setAdapter(adapter);
    songListView.setOnItemClickListener(this);
    if (controller == null)setController();
    return rootView;
}

private BroadcastReceiver mMusicServiceReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        MusicService ms = (MusicService) intent.getSerializableExtra(Constants.ACTION.MUSIC_SERVICE_EXTRA);
        if (ms != null) Log.v(LOG_TAG, "MusicService received-------------------------------");
        else Log.v(LOG_TAG, "MusicService not received----------------------------");
        musicService = ms;
        musicService.setList(songList);
    }
};

这是您在ImageButton上设置所选/捕获图像的方法。

希望这会有所帮助。