Created
December 28, 2013 14:55
-
-
Save moltak/8160257 to your computer and use it in GitHub Desktop.
android camera picture pickup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.startuphouse.wallposter; | |
import android.content.ClipData; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.provider.MediaStore; | |
import android.view.View; | |
import android.content.Intent; | |
import android.widget.AdapterView; | |
import android.widget.EditText; | |
import android.widget.GridView; | |
import android.widget.Toast; | |
import com.actionbarsherlock.app.SherlockActivity; | |
import com.actionbarsherlock.view.Menu; | |
import com.actionbarsherlock.view.MenuInflater; | |
import com.actionbarsherlock.view.MenuItem; | |
import com.squareup.otto.Subscribe; | |
import com.startuphouse.wallposter.Preference.AccountStorage; | |
import com.startuphouse.wallposter.Utilities.Utilities; | |
import com.startuphouse.wallposter.adapter.SelectedImageAdapter; | |
import com.startuphouse.wallposter.dialog.ProgressDialogFactory; | |
import com.startuphouse.wallposter.network.Network; | |
import com.startuphouse.wallposter.network.NetworkBus; | |
import com.startuphouse.wallposter.network.packet.UploadPacket; | |
import java.util.ArrayList; | |
/** | |
* Created by moltak on 12/26/13. | |
*/ | |
public class ActivityUpload extends SherlockActivity { | |
private static final int LOAD_IMAGE_RESULTS = 1; | |
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; | |
private ArrayList<String> images; | |
private SelectedImageAdapter adapter; | |
private ProgressDialogFactory dialog; | |
private Uri cameraPictureUri; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_upload); | |
getSupportActionBar().setTitle("대자보 작성하기"); | |
getSupportActionBar().setHomeButtonEnabled(true); | |
getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
images = new ArrayList<String>(); | |
adapter = new SelectedImageAdapter(getLayoutInflater()); | |
((GridView)findViewById(R.id.gridview_selectimages)).setAdapter(adapter); | |
((GridView)findViewById(R.id.gridview_selectimages)).setOnItemClickListener(new GridItemClicked()); | |
dialog = new ProgressDialogFactory(); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
NetworkBus.getInstance().register(this); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
NetworkBus.getInstance().unregister(this); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
MenuInflater menuInflater = new MenuInflater(this); | |
menuInflater.inflate(R.menu.upload, menu); | |
return super.onCreateOptionsMenu(menu); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case android.R.id.home: | |
finish(); | |
break; | |
case R.id.menu_upload: | |
onUploadMenuClicked(); | |
break; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
private void onUploadMenuClicked() { | |
String title = ((EditText)findViewById(R.id.edittext_upload_title)).getText().toString(); | |
String contents = ((EditText)findViewById(R.id.edittext_upload_contents)).getText().toString(); | |
if(title.length() == 0 || contents.length() == 0) { | |
Toast.makeText(this, "내용을 입력해주세요.", Toast.LENGTH_LONG).show(); | |
} | |
else { | |
dialog.showDialog(this); | |
Network network = new Network(this); | |
network.uploadWallPoster(title, contents, images, AccountStorage.getSession(this)); | |
} | |
} | |
@Subscribe | |
public void onUploadResult(UploadPacket packet) { | |
dialog.dismissDialog(); | |
Toast.makeText(this, packet.getResponseMessage(), Toast.LENGTH_LONG).show(); | |
if(packet.getResponseCode() == 0) { | |
((EditText)findViewById(R.id.edittext_upload_title)).setText(""); | |
((EditText)findViewById(R.id.edittext_upload_contents)).setText(""); | |
images.clear(); | |
adapter.setImages(images); | |
adapter.notifyDataSetChanged(); | |
} | |
} | |
public void onImageSelectBtnClicked(View view) { | |
if(images.size() >= 4) { | |
Toast.makeText(this, "사진은 한번에 최대 4장까지 업로드할 수 있습니다.", Toast.LENGTH_LONG).show(); | |
return; | |
} | |
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); | |
startActivityForResult(i, LOAD_IMAGE_RESULTS); | |
} | |
public void onCameraBtnClicked(View view) { | |
if(images.size() >= 4) { | |
Toast.makeText(this, "사진은 한번에 최대 4장까지 업로드할 수 있습니다.", Toast.LENGTH_LONG).show(); | |
return; | |
} | |
// create Intent to take a picture and return control to the calling application | |
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
cameraPictureUri = Utilities.getOutputMediaFileUri(Utilities.MEDIA_TYPE_IMAGE); // create a file to save the image | |
intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPictureUri); // set the image file name | |
// start the image capture Intent | |
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
// Here we need to check if the activity that was triggers was the Image Gallery. | |
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value. | |
// If the resultCode is RESULT_OK and there is some data we know that an image was picked. | |
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) { | |
// Let's read picked image data - its URI | |
ClipData pickedImage = data.getClipData(); | |
// Let's read picked image path using content resolver | |
String[] filePath = { MediaStore.Images.Media.DATA }; | |
int maxImageCount = pickedImage.getItemCount() > 4 ? 4 : pickedImage.getItemCount(); | |
for(int i = 0; i < maxImageCount; i ++) { | |
addImageToList(pickedImage.getItemAt(i).getUri()); | |
} | |
adapter.setImages(images); | |
adapter.notifyDataSetChanged(); | |
} | |
else if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { | |
if (resultCode == RESULT_OK && cameraPictureUri != null) { | |
images.add(cameraPictureUri.getPath()); | |
adapter.setImages(images); | |
adapter.notifyDataSetChanged(); | |
} else if (resultCode == RESULT_CANCELED) { | |
} else { | |
// Image capture failed, advise user | |
} | |
} | |
} | |
private void addImageToList(Uri uri) { | |
String[] filePath = { MediaStore.Images.Media.DATA }; | |
Cursor cursor = getContentResolver().query(uri, filePath, null, null, null); | |
cursor.moveToFirst(); | |
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0])); | |
images.add(imagePath); | |
cursor.close(); | |
} | |
private class GridItemClicked implements AdapterView.OnItemClickListener { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
Toast.makeText(getBaseContext(), (String)view.getTag(), Toast.LENGTH_LONG).show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment