我需要从用户在图库中点击的位图中获取信息,然后返回到我的页面。我可以恢复图像。我需要得到: 以字节,宽度,高度和文件路径为单位的大小,并将其返回到文本视图。
以下是我调用图库视图并选择图片的代码:
package com.example.name;
import statements here
public class ImageCaptureActivity extends Activity {
//1st version
private static final int CAMERA_RESULT = 5;
private static final int GALLERY_RESULT = 6;
private Bitmap imageResult;
private ImageView idImageView1;
private Bitmap selectedImage;
private ImageView imgShowResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_capture);
//get a reference to the image view that will display plant photo
//idImageView1 = (ImageView) findViewById(R.id.idImageView1);
//get access to gallery image picked- on this layout: idImageView1
imgShowResult = (ImageView) findViewById(R.id.idImageView1);
}
private void getSystemService() {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.image_capture, menu);
return true;
}
//Gallery view method
public void onViewGalleryClicked(View v){
//testbuttons-Toast.makeText(this,"you clicked the View Gallery button",Toast.LENGTH_LONG).show();
//Implicit the line below says what we want to do, not where we want to go.
//pick an image from a gallery, so specify an image pick
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
//give me the path (file system directory) where we store images:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();
//convert to URI, as our gallery expects this, and convert to new local variable
Uri picturesDirectory = Uri.parse(path);
// set the data and the type on this intent,
//so we tell it where to look for files and what files types we want
photoPickerIntent.setDataAndType(picturesDirectory, "image/*");
// start activity and tell it we want a result
//line below need to create a constant on the 6 ctrl-space I think, create constant
startActivityForResult(photoPickerIntent, GALLERY_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
if (resultCode == RESULT_OK ){
//ONLY DO IF RESULT OK
if (requestCode == CAMERA_RESULT){
imageResult = (Bitmap) data.getExtras().get("data");
imgShowResult.setImageBitmap(imageResult);
}
else if (requestCode == GALLERY_RESULT){
//anything returned is returned through the get data object
//find the path of the selected image
Uri photoLocation = data.getData();
Intent resultIntent = new Intent (this, ImageInformationActivity.class);
resultIntent.putExtra("imagePath", photoLocation);
startActivityForResult(resultIntent,10);
}
}
}
}
以下是我的接收页面代码:
package com.example.namehere;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ImageInformationActivity extends Activity {
//public static final String PLANT_RESULT = "PLANT_RESULT";
private static final String IMAGE_FILENAME = null;
private Bitmap selectedImage;
private ImageView imgShowResult;
private Uri photoLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_information);
//String searchTerm = getIntent().getStringExtra(ImageCaptureActivity.SEARCH_PLANT_NAME);
imgShowResult = (ImageView) findViewById(R.id.imageView2);
try {
photoLocation = (Uri)getIntent().getParcelableExtra("imagePath");
//a stream of data from a tile
InputStream openInputStream = getContentResolver().openInputStream(photoLocation);
//take a stream of data and convert to bitmap
selectedImage = BitmapFactory.decodeStream(openInputStream);
//assign this image to our image view on this page
imgShowResult.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//alert the user that something went wrong.
Toast.makeText(this, getString(R.string.unable_to_open_image), Toast.LENGTH_LONG).show();
}
// Bundle fileInfo = new Bundle();
// photoLocation = findViewById(R.id.idLocation);
//plantPlacesColor.putExtras(fileInfo);
//=(TextView) findViewById(R.id.idHeight);
//=(TextView) findViewById(R.id.idWidth);
//=(TextView) findViewbyId(R.id.idBytes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.image_information, menu);
return true;
}
}
这是我的xml页面
<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=".ImageInformationActivity" >
<TextView
android:id="@+id/idBytes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/idHeight"
android:layout_below="@+id/idHeight"
android:layout_marginTop="31dp"
android:text="@string/lblBytes"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/idHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/idWidth"
android:layout_below="@+id/idWidth"
android:layout_marginTop="22dp"
android:text="@string/lblHeight"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/idLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="@string/lblLocation"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RatingBar
android:id="@+id/ratingBarImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView2"
android:layout_alignParentBottom="true"
android:layout_marginBottom="22dp" />
<TextView
android:id="@+id/idLike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ratingBarImage"
android:layout_alignParentBottom="true"
android:layout_marginBottom="22dp"
android:text="@string/doLike"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/idBytes"
android:layout_below="@+id/idBytes"
android:layout_marginTop="27dp"
android:src="@android:drawable/gallery_thumb" />
<TextView
android:id="@+id/idWidth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/idLocation"
android:layout_below="@+id/idLocation"
android:layout_marginTop="15dp"
android:text="@string/lblWidth"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
我知道我应该使用Bitmap Factory,我知道我应该使用findViewById ......等等。例如:
//=(TextView) findViewById(R.id.idHeight);
//=(TextView) findViewById(R.id.idWidth);
//=(TextView) findViewbyId(R.id.idBytes);
但是我被卷入了细节。
答案 0 :(得分:2)
Bitmap
你可以获得字节,宽度,高度的大小,但不能获得文件的路径,因为位图不是文件,而是存储在内存中的图像。
获取此信息:
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int sizeBytes = getSizeFromBitmap(bitmap);
TextView textWidth = (TextView) findViewById(R.id.idWidth);
textWidth.setText(String.valueOf(width));
// ..continue
使用此方法计算大小:
public static int getSizeFromBitmap(Bitmap bitmap) {
int pixels = bitmap.getHeight() * bitmap.getWidth();
int bytesPerPixel = 0;
switch (bitmap.getConfig()) {
case ARGB_8888:
bytesPerPixel = 4;
break;
case RGB_565:
bytesPerPixel = 2;
break;
case ARGB_4444:
bytesPerPixel = 2;
break;
case ALPHA_8:
bytesPerPixel = 1;
break;
}
return pixels * bytesPerPixel;
}
之后,您可以将值设置为右TextView
。注意:您可以在int
的方法setText
内设置TextView
,因为它只接受字符串资源的ID。您必须先将数字转换为String
才能设置它。希望它有所帮助。