Skip to content

Commit

Permalink
Merge pull request #468 from ZaakLim/master
Browse files Browse the repository at this point in the history
#89 #82 实验3
  • Loading branch information
zengsn committed Apr 27, 2016
2 parents 384cf83 + 16e9ca7 commit 3fe308a
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package edu.hzuapps.androidworks.homeworks.net1314080903125;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Net1314080903125Activity extends AppCompatActivity {

Expand Down Expand Up @@ -35,7 +47,68 @@ public void onClick(View v){
}
});
}
private Uri saveBitmap(Bitmap bm)
{
File tmpDir = new File(Environment.getExternalStorageDirectory() +"/com.zaak.zaaklim");
if(!tmpDir.exists())
{
tmpDir.mkdir();
}
File img = new File(tmpDir.getAbsolutePath() +"zaaklim.png");
try{
FileOutputStream fos = new FileOutputStream(img);
bm.compress(Bitmap.CompressFormat.PNG,85,fos);
fos.flush();
fos.close();
return Uri.fromFile(img);
}catch (FileNotFoundException e){
e.printStackTrace();
return null;
}catch(IOException e){
e.printStackTrace();
return null;
}
}

private Uri convertUri(Uri uri)
{
InputStream is = null;
try{
is = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
return saveBitmap(bitmap);
}catch (FileNotFoundException e){
e.printStackTrace();
return null;
}catch (IOException e){
e.printStackTrace();
return null;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST_CODE) {
if (data == null) {
return;
} else {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap bm = extras.getParcelable("data");
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bm);
}
}
} else if (requestCode == GALLERY_REQUEST_CODE) {
if (data == null) {
return;
}
Uri uri;
uri = data.getData();
Toast.makeText(Net1314080903125Activity.this, uri.toString(), Toast.LENGTH_LONG).show();

}
}

}
Binary file added app/src/main/res/drawable/net1314080903125.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions app/src/main/res/layout/net1314080903125.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Net1314080903125Activity">

<ImageView
android:layout_width="400px"
android:layout_height="400px"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:src="@drawable/net1314080903125"/>


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="摄像头"
android:id="@+id/btn_camera"
android:layout_marginTop="80dp"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 图库 "
android:id="@+id/btn_gallery"
android:layout_below="@+id/btn_camera"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp" />
</RelativeLayout>

0 comments on commit 3fe308a

Please sign in to comment.