我在一个文件中有一个大位图(比如3888x2592).现在,我想将该位图的大小调整为800x533并将其保存到另一个文件中.我通常会通过调用Bitmap.createBitmap
方法来缩放位图,但它需要一个源位图作为第一个参数,我无法提供,因为将原始图像加载到Bitmap对象当然会超出内存(例如,请参见此处).
我也无法读取位图,例如,BitmapFactory.decodeFile(file, options)
提供a BitmapFactory.Options.inSampleSize
,因为我想将其调整为精确的宽度和高度.使用inSampleSize
会将位图的大小调整为972x648(如果我使用inSampleSize=4
)或778x518(如果我使用inSampleSize=5
,它甚至不是2的幂).
我还想避免使用inSampleSize在第一步中使用例如972x648读取图像,然后在第二步中将其大小调整为800x533,因为与直接调整原始图像大小相比,质量会很差.
总结我的问题:是否有办法读取10MP或更高的大图像文件并将其保存到新的图像文件,调整大小到特定的新宽度和高度,而不会得到OutOfMemory异常?
我也尝试BitmapFactory.decodeFile(file, options)
手动将Options.outHeight和Options.outWidth值设置为800和533,但它不起作用.
当我将媒体库中的图像加载到位图时,一切都运行正常,除了在垂直拿着手机时用相机拍摄的图片被旋转,以便我总是得到水平图片,即使它在画廊.为什么这样,我怎么能正确加载它?
我想通过按下按钮将常量字符串插入EditText.该字符串应插入EditText中的当前位置.如果我使用EditText.append
文本在EditText的末尾插入.
我怎样才能做到这一点?我找不到合适的方法.
我正在为我的Android应用设置OAuth.为了测试它我执行了以下操作:在我的项目中添加了signpost-core-1.2.1.1.jar和signpost-commonshttp4-1.2.1.1.jar,添加了变量"CommonsHttpOAuthConsumer consumer"和"CommonsHttpOAuthProvider provider"并在执行时执行以下操作单击按钮:
consumer = new CommonsHttpOAuthConsumer("xxx", "yyy");
provider = new CommonsHttpOAuthProvider("https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize");
oauthUrl = provider.retrieveRequestToken(consumer, "myapp://twitterOauth");
persistOAuthData();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(oauthUrl)));
Run Code Online (Sandbox Code Playgroud)
persistOAuthData()执行以下操作:
protected void persistOAuthData()
{
try
{
FileOutputStream providerFOS = this.openFileOutput("provider.dat", MODE_PRIVATE);
ObjectOutputStream providerOOS = new ObjectOutputStream(providerFOS);
providerOOS.writeObject(this.provider);
providerOOS.close();
FileOutputStream consumerFOS = this.openFileOutput("consumer.dat", MODE_PRIVATE);
ObjectOutputStream consumerOOS = new ObjectOutputStream(consumerFOS);
consumerOOS.writeObject(this.consumer);
consumerOOS.close();
}
catch (Exception e) { }
}
Run Code Online (Sandbox Code Playgroud)
所以,消费者和供应商正在打开浏览器,如之前描述保存在这里.
在onResume()方法中,我加载提供者和消费者数据并执行以下操作:
Uri uri = this.getIntent().getData();
if (uri != null && uri.getScheme().equals("myapp") && uri.getHost().equals("twitterOauth"))
{
verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); …
Run Code Online (Sandbox Code Playgroud) 我在清单中声明了一个WebView活动,如下所示:
<activity android:name=".MyWebView"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.Dialog">
</activity>
Run Code Online (Sandbox Code Playgroud)
WebView看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
当我在主活动中启动此活动时,只有包含应用程序名称的对话框标题可见,但WebView不可见.如果我将一个TextView添加到LinearLayout,它也会显示,但仍然缺少WebView.如果我不在android:theme="@android:style/Theme.Dialog"
清单中应用,则显示WebView.
为什么这样,我如何在对话框中显示WebView?
我已经描述了一个自定义的按钮栏这里.
现在,我想在第一个和第二个之间以及第二个和第三个按钮之间添加一个分隔符.我的按钮栏定义如下:
<LinearLayout
android:id="@+id/buttonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/buttonbarstyle"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
>
<ImageButton
android:id="@+id/buttonBarImageButton1"
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:src="@drawable/copy"
android:padding="2dip"
android:background="@drawable/buttonbar_background_selector"
android:layout_gravity="center_vertical"
/>
<ImageButton
android:id="@+id/buttonBarImageButton2"
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:src="@drawable/options"
android:padding="2dip"
android:background="@drawable/buttonbar_background_selector"
android:layout_gravity="center_vertical"
/>
<ImageButton
android:id="@+id/buttonBarImageButton3"
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:src="@drawable/media_play"
android:padding="2dip"
android:background="@drawable/buttonbar_background_selector"
android:layout_gravity="center_vertical"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
任务似乎很简单,但我找不到一个好方法.它应该在每个按钮之间有一个灰色的分隔符,所以它看起来有点像这样.
我相信这很简单,请指出我正确的方向.
在我的Android 2.2.2设备上,画廊看起来非常好.我想在自己的应用程序中做的是按一个按钮,然后显示如下所示的菜单:
这是使用任何标准的Android主题/样式?有没有人知道有这样一个菜单的示例代码?
编辑:我发现用一个Dialog可以模仿这个菜单.为了简化这个例子,我在这个例子中没有使用ListView,只是对话框的一个TextView条目:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
android:padding="10dp"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
按下按钮时显示对话框:
Dialog dialog = new Dialog(MyActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Test option 1");
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
WMLP.gravity = (Gravity.BOTTOM | Gravity.LEFT);
WMLP.x = 0;
WMLP.y = 0;
dialog.getWindow().setAttributes(WMLP);
dialog.show();
Run Code Online (Sandbox Code Playgroud)
这将创建一个靠近图片菜单的对话框.不过我还有两个问题:
1)如何在对话框底部绘制这个小三角形,如上图所示?
2)应该打开对话框的按钮水平放置在底部按钮栏的中间.因此,当我按下它时,对话框应显示在该按钮的正上方.我想做的是:
WMLP.x = middleButton.getLeft() + (middleButton.getWidth() / 2) - dialog.getWindow().getDecorView().getPaddingLeft() - (WMLP.width / 2);
Run Code Online (Sandbox Code Playgroud)
问题是,WMLP.width是-2.我想原因是布局宽度设置为"wrap_content"(即-2),此时不知道实际宽度.那么,我如何确定对话框的宽度,以便我可以将它同心放在另一个视图上?
更新:我终于找到了一个很好的对话源,如下所示:http: //www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/
这正是我想要的,我现在正在我的应用程序中使用它.
在我的应用程序中,我首先让用户选择一个视频:
startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("video/*"), ACTIVITY_PICKVIDEO);
Run Code Online (Sandbox Code Playgroud)
然后我确定视频ID:
fileID = Integer.parseInt(contentUri.getLastPathSegment());
Run Code Online (Sandbox Code Playgroud)
因此,视频content://media/external/video/media/5
的ID为5.
然后我尝试获取缩略图位图:
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, fileID, MediaStore.Video.Thumbnails.MICRO_KIND, options);
Run Code Online (Sandbox Code Playgroud)
抛出没有异常,但位图的宽度和高度为-1.我不确定所需的ID getThubnail()
是否实际上是我上面确定的ID.
有没有人知道如果你有内容Uri如何获取缩略图位图的工作示例?
有趣的是(也许是这样)当我尝试使用MediaStore.Video.Thumbnails.MINI_KIND
缩略图大小和尝试时,我会得到null .IllegalArgumentException ("Unsupported kind: 2")
FULL_SCREEN_KIND
我正在使用Android 2.1的摩托罗拉里程碑.
编辑: 我也尝试通过查询BaseColumns._ID来获取ID,但结果与Uri相同(在给定的示例中,_ID为5).